Overview
Using a Auth.js / NextAuth.js adapter you can connect to any database service or even several different services at the same time. The following listed official adapters are created and maintained by the community:
Dgraph Adapter
DynamoDB Adapter
Fauna Adapter
Firebase Adapter
Mikro ORM Adapter
MongoDB Adapter
Neo4j Adapter
PouchDB Adapter
Prisma Adapter
Sequelize Adapter
Supabase Adapter
TypeORM Adapter
Upstash Adapter
Xata Adapter
If you don't find an adapter for the database or service you use, you can always create one yourself. Have a look at our guide on how to create a database adapter.
Models
Auth.js can be used with any database. Models tell you what structures Auth.js expects from your database. Models will vary slightly depending on which adapter you use, but in general, will look something like this. Each adapter's model/schema will be slightly adapted for its needs, but will look very much like this schema below:
More information about each Model / Table can be found below.
You can create your own adapter if you want to use Auth.js with a database that is not supported out of the box, or you have to change fields on any of the models.
User
The User model is for information such as the user's name and email address.
Email address is optional, but if one is specified for a User then it must be unique.
If a user first signs in with OAuth then their email address is automatically populated using the one from their OAuth profile, if the OAuth provider returns one.
This provides a way to contact users and for users to maintain access to their account and sign in using email in the event they are unable to sign in with the OAuth provider in future (if the Email Provider is configured).
User creation in the database is automatic, and happens when the user is logging in for the first time with a provider. The default data saved is id
, name
, email
and image
. You can add more profile data by returning extra fields in your OAuth provider's profile()
callback.
Account
The Account model is for information about OAuth accounts associated with a User. It will usually contain access_token
, id_token
and other OAuth specific data. TokenSet
from openid-client
might give you an idea of all the fields.
In case of an OAuth 1.0 provider (like Twitter), you will have to look for oauth_token
and oauth_token_secret
string fields. GitHub also has an extra refresh_token_expires_in
integer field. You have to make sure that your database schema includes these fields.
A single User can have multiple Accounts, but each Account can only have one User.
Linking Accounts to Users happen automatically, only when they have the same e-mail address, and the user is currently signed in. Check the FAQ for more information why this is a requirement.
You can manually unlink accounts, if your adapter implements the unlinkAccount
method. Make sure to take all the necessary security steps to avoid data loss.
Linking and unlinking accounts through an API is a planned feature: https://github.com/nextauthjs/next-auth/issues/230
Session
The Session model is used for database sessions. It is not used if JSON Web Tokens are enabled. Keep in mind, that you can use a database to persist Users and Accounts, and still use JWT for sessions. See the session.strategy
option.
A single User can have multiple Sessions, each Session can only have one User.
When a Session is read, we check if it's expires
field indicates an invalid session, and delete it from the database. You can also do this clean-up periodically in the background to avoid our extra delete call to the database during an active session retrieval. This might result in a slight performance increase in a few cases.
Verification Token
The Verification Token model is used to store tokens for passwordless sign in.
A single User can have multiple open Verification Tokens (e.g. to sign in to different devices).
It has been designed to be extendable for other verification purposes in the future (e.g. 2FA / short codes).
Auth.js makes sure that every token is usable only once, and by default has a short (1 day, can be configured by maxAge
) lifetime. If your user did not manage to finish the sign-in flow in time, they will have to start the sign-in process again.
Due to users forgetting or failing at the sign-in flow, you might end up with unwanted rows in your database, that you might have to periodically clean up to avoid filling the database up with unnecessary data.
RDBMS Naming Convention
Auth.js / NextAuth.js uses camelCase
for its own database rows, while respecting the conventional snake_case
formatting for OAuth related values. If mixed casing is an issue for you, most adapters have a dedicated section on how to use a single naming convention.
TypeScript
Check out the @auth/core/adapters
API Reference documentation.