Skip to main content
Version: v4

Sequelize

This is the Sequelize Adapter for next-auth.

Getting Started

  1. Install the necessary packages
npm install next-auth @next-auth/sequelize-adapter sequelize
danger

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

  1. Add this adapter to your pages/api/auth/[...nextauth].js next-auth configuration object.
pages/api/auth/[...nextauth].js
import NextAuth from "next-auth"
import SequelizeAdapter from "@next-auth/sequelize-adapter"
import { Sequelize } from "sequelize"

// https://sequelize.org/master/manual/getting-started.html#connecting-to-a-database
const sequelize = new Sequelize("yourconnectionstring")

// For more information on each option (and a full list of options) go to
// https://next-auth.js.org/configuration/options
export default NextAuth({
// https://next-auth.js.org/providers/overview
providers: [],
adapter: SequelizeAdapter(sequelize),
})

Updating the database 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

import NextAuth from "next-auth"
import SequelizeAdapter from "@next-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 default NextAuth({
...
adapter
...
})

Using custom models

Sequelize models are option to customization like so:

import NextAuth from "next-auth"
import SequelizeAdapter, { models } from "@next-auth/sequelize-adapter"
import Sequelize, { DataTypes } from "sequelize"

const sequelize = new Sequelize("sqlite::memory:")

export default NextAuth({
// https://next-auth.js.org/providers/overview
providers: [],
adapter: SequelizeAdapter(sequelize, {
models: {
User: sequelize.define("user", {
...models.User,
phoneNumber: DataTypes.STRING,
}),
},
}),
})