Basic Usage
Better Auth provides built-in authentication support for:
- Email and password
- Social provider (Google, GitHub, Apple, and more)
But also can easily be extended using plugins, such as: username, magic link, passkey, email-otp, and more.
Email & Password
To enable email and password authentication:
Sign Up
To sign up a user you need to call the client method signUp.email
with the user's information.
By default, the users are automatically signed in after they successfully sign up. To disable this behavior you can set autoSignIn
to false
.
Sign In
To sign a user in, you can use the signIn.email
function provided by the client.
Always invoke client methods from the client side. Don't call them from the server.
Server-Side Authentication
To authenticate a user on the server, you can use the auth.api
methods.
If the server cannot return a response object, you'll need to manually parse and set cookies. But for frameworks like Next.js we provide a plugin to handle this automatically
Social Sign-On
Better Auth supports multiple social providers, including Google, GitHub, Apple, Discord, and more. To use a social provider, you need to configure the ones you need in the socialProviders
option on your auth
object.
Signin with social providers
To sign in using a social provider you need to call signIn.social
. It takes an object with the following properties:
You can also authenticate using idToken
or accessToken
from the social provider instead of redirecting the user to the provider's site. See social providers documentation for more details.
Signout
To signout a user, you can use the signOut
function provided by the client.
you can pass fetchOptions
to redirect onSuccess
Session
Once a user is signed in, you'll want to access the user session. Better Auth allows you easily to access the session data from the server and client side.
Client Side
Use Session
Better Auth provides a useSession
hook to easily access session data on the client side. This hook is implemented using nanostore and has support for each supported framework and vanilla client, ensuring that any changes to the session (such as signing out) are immediately reflected in your UI.
Get Session
If you prefer not to use the hook, you can use the getSession
method provided by the client.
You can also use it with client-side data-fetching libraries like TanStack Query.
Server Side
The server provides a session
object that you can use to access the session data. It requires request headers object to be passed to the getSession
method.
Example: Using some popular frameworks
For more details check session-management documentation.
Using Plugins
One of the unique features of Better Auth is a plugins ecosystem. It allows you to add complex auth related functionality with small lines of code.
Below is an example of how to add two factor authentication using two factor plugin.
Server Configuration
To add a plugin, you need to import the plugin and pass it to the plugins
option of the auth instance. For example, to add two factor authentication, you can use the following code:
now two factor related routes and method will be available on the server.
Migrate Database
After adding the plugin, you'll need to add the required tables to your database. You can do this by running the migrate
command, or by using the generate
command to create the schema and handle the migration manually.
generating the schema:
using the migrate
command:
If you prefer adding the schema manually, you can check the schema required on the two factor plugin documentation.
Client Configuration
Once we're done with the server, we need to add the plugin to the client. To do this, you need to import the plugin and pass it to the plugins
option of the auth client. For example, to add two factor authentication, you can use the following code:
now two factor related methods will be available on the client.
Next step: See the the two factor plugin documentation.