Gettig Started with Better Auth
Install the Package
Let's start by adding Better Auth to your project:
If you're using a separate client and server setup, make sure to install Better Auth in both parts of your project. (currnelty better auth only supports web platforms more clients will be added soon)
Set Environment Variables
Create a .env
file in the root of your project and add the following environment variables:
- Secret Key
Random value used by the library for encryption and generating hashes. You can generate one using the button below or you can use something like openssl.
- Set Base URL (optional)
Create A Better Auth Instance
Create a file named auth.ts
in one of these locations:
- Project root
lib/
folderutils/
folder
You can also nest any of these folders under src/
folder. (e.g. src/lib/auth.ts
)
And in this file, import Better Auth and create your instance.
Make sure to export the auth instance with the variable name auth
or as a default
export.
Configure Database
Better Auth requires a database to store user data. It uses Kysely under the hood to connect to your database.
postgresql
, mysql
, and sqlite
are supported out of the box.
You can also pass any dialect that is supported by Kysely to the database configration.
Example with LibsqlDialect:
Currently, Better Auth only support databases that are supported by Kysely. vote this issue if you like to see non sql db support
Migrate Schema
Better Auth includes a CLI tool to migrate the required schema to your database. It introspects the database and creates the required tables. Run the following command to perform the migration:
Better Auth automatically sets up the following tables in your database if they don't already exist:
user
session
account
For more details head over to schema section.
Authentication Methods
Configure the authentication methods you want to use. Better auth comes with built-in support for email/password, and social sign-on providers.
You can use even more authentication methods like passkey, username, magic link and more through plugins.
Mount Handler
To handle api requests, you need to set up a route handler on your server.
Create a new file or route in your framework's designated catch-all route handler. This route should handle requests for the path /api/auth/*
(unless you've configured a different base path).
Better auth supports any backend framework with standard Request and Response objects and offers helper functions for popular frameworks.
Create Client Instance
The client-side library helps you interact with the auth server. Better Auth comes with a client for all the popular web frameworks inlcuding for vanilla javascript.
- Import
createAuthClient
from the package for your framework (e.g., "better-auth/react" for React). - Call the function to create your client.
- Pass the base url of your auth server. (If the auth server is running on the same domain as your client, you can skip this step.)
If you're using a differnt base path other than /api/auth
make sure to pass the whole url inlcuding the path. (e.g. http://localhost:3000/custom-path/auth
)
Tip: You can also export specific methods if you prefer:
🎉 That's it!
That's it! You're now ready to use better-auth in your application. Continue to basic usage to learn how to use the auth instance to sign in users.