Database Adapters
An Adapter in NextAuth.js connects your application to whatever database or backend system you want to use to store data for user accounts, sessions, etc.
You do not need to specify an adapter explicitly unless you want to use advanced options such as custom models or schemas, if you want to use the Prisma adapter instead of the default TypeORM adapter, or if you are creating a custom adapter to connect to a database that is not one of the supported databases.
#
Database SchemasConfigure your database by creating the tables and columns to match the schema expected by NextAuth.js.
#
TypeORM AdapterNextAuth.js comes with a default adapter that uses TypeORM so that it can be used with many different databases without any further configuration, you simply add the node module for the database driver you want to use to your project and pass a database connection string to NextAuth.js.
The default adapter is the TypeORM adapter, the following configuration options are exactly equivalent.
The tutorial Custom models with TypeORM explains how to extend the built in models and schemas used by the TypeORM adapter. You can use these models in your own code.
tip
The synchronize
option in TypeORM will generate SQL that exactly matches the documented schemas for MySQL and Postgres.
However, it should not be enabled against production databases as it may cause data loss if the configured schema does not match the expected schema!
#
Prisma AdapterYou can also use NextAuth.js with the experimental adapter for Prisma 2.
To use this adapter, you need to install Prisma Client and Prisma CLI:
Configure your NextAuth.js to use the Prisma adapter:
tip
While Prisma includes an experimental feature in the migration command that is able to generate SQL from a schema, creating tables and columns using the provided SQL is currently recommended instead as SQL schemas automatically generated by Prisma may differ from the recommended schemas.
#
Prisma SchemaCreate a schema file in prisma/schema.prisma
similar to this one:
note
Set the datasource
option appropriately for your database:
#
Generate ClientOnce you have saved your schema, use the Prisma CLI to generate the Prisma Client:
To configure you database to use the new schema (i.e. create tables and columns) use the prisma migrate
command:
To generate a schema in this way with the above example code, you will need to specify your datbase connection string in the environment variable DATABASE_URL
. You can do this by setting it in a .env
file at the root of your project.
As this feature is experimental in Prisma, it is behind a feature flag. You should check your database schema manually after using this option. See the Prisma documentation for information on how to use prisma migrate
.
#
Custom ModelsYou can add properties to the schema and map them to any database column names you wish, but you should not change the base properties or types defined in the example schema.
The model names themselves can be changed with a configuration option, and the datasource can be changed to anything supported by Prisma.
You can use custom model names by using the modelMapping
option (shown here with default values).
tip
If you experience issues with Prisma opening too many database connections in local development mode (e.g. due to Hot Module Reloading) you can use an approach like this when initalising the Prisma Client:
#
Custom AdapterSee the tutorial for creating a database adapter for more information on how to create a custom adapter. Have a look at the adapters repository to see community maintained custom adapters or add your own.