Installation
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.
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/
, app/
or server/
folder. (e.g. src/lib/auth.ts
, app/lib/auth.ts
).
And in this file, import Better Auth and create your auth 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. By default, it uses Kysely for database queries, supporting postgresql
, mysql
, and sqlite
out of the box.
You can also use any Kysely dialect in the database configuration.
Example with LibsqlDialect:
Adapters
If your database isnβt supported by Kysely, you can use an adapter to connect. Simply import the adapter and pass it into the database
option.
For optimal performance, use the built-in Kysely adapter if possible.
Create Database Tables
Better Authβs comes with a CLI tool to help manage schema required by the library.
Generate: Generates an ORM schema or SQL migration file.
Migrate: Directly creates the required tables in the database. (only available for built-in Kysely adapter)
see the CLI documentation for more information.
If you instead want to create the schema manually, you can find the core schema required in the database 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, including 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 different base path other than /api/auth
make sure to pass the whole url including 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.