Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
Getting Started
Adapters
Sequelize

Sequelize Adapter

Resources

Setup

Installation

npm install @auth/sequelize-adapter sequelize

Environment Variables

DATABASE_URL=postgres://postgres:adminadmin@0.0.0.0:5432/db

Configuration

⚠️

You’ll also have to manually install the driver for your database of choice.

./auth.ts
import NextAuth from "next-auth"
import SequelizeAdapter from "@auth/sequelize-adapter"
import { Sequelize } from "sequelize"
 
const sequelize = new Sequelize(process.env.DATABASE_URL)
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  providers: [],
  adapter: SequelizeAdapter(sequelize),
})

Schema

By default, the sequelize adapter will not create tables in your database. In production, best practice is to create the required tables in your database via migrations. In development, you are able to call sequelize.sync() to have sequelize create the necessary tables, foreign keys and indexes:

This schema is adapted for use in Sequelize and based upon our main schema

./auth.ts
import NextAuth from "next-auth"
import SequelizeAdapter from "@auth/sequelize-adapter"
import Sequelize from "sequelize"
 
const sequelize = new Sequelize("sqlite::memory:")
const adapter = SequelizeAdapter(sequelize)
 
// Calling sync() is not recommended in production
sequelize.sync()
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter,
})

Advanced usage

Using custom models

Sequelize models are option to customization like so:

./auth.ts
import NextAuth from "next-auth"
import SequelizeAdapter, { models } from "@auth/sequelize-adapter"
import Sequelize, { DataTypes } from "sequelize"
 
const sequelize = new Sequelize("sqlite::memory:")
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  // https://authjs.dev/reference/providers/
  providers: [],
  adapter: SequelizeAdapter(sequelize, {
    models: {
      User: sequelize.define("user", {
        ...models.User,
        phoneNumber: DataTypes.STRING,
      }),
    },
  }),
})
Auth.js © Balázs Orbán and Team - 2024