Before a user can sign in, they need to sign up. To sign up a user using email and password, you need to call the client method signUp.email with the user's information.
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.
Once a user is signed in, you'll want to access their session. Better auth allows you easily to access the session data from the server and client side.
Better Auth provides a useSession hook to easily access session data on the client side. This hook is implemented in a reactive way for each supported framework, ensuring that any changes to the session (such as signing out) are immediately reflected in your UI.
It has the following properties:
data: the actual session data which includes session and user object.
isPending: a boolean that indicates whether the session is being loaded.
error: an error object that contains any errors that occurred while loading the session.
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.
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 facor authentication, you can use the following code:
now two factor related routes and method will be available on the server.
once you have added the plugin, you need to migrate your database to add the necessary tables and fields. You can do this by running the following command:
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 facor authentication, you can use the following code:
now two factor related methods will be available on the client.