npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

next-auth-sequelize-adapter-2

v2.0.0

Published

Next-Auth adapter for Sequelize ORM. Based on Rafay Khans work

Downloads

5

Readme

Sequelize Adapter For Next-Auth

GitHub issues GitHub forks GitHub stars

Getting Started

To get started, either clone the example or use $ npx create-next-app my-app with the sequelize-cli's $ sequelize init command and generate all the necessary files.

Prerequisites

You must install sequelize in your Next-JS/Next-Auth project to use this adapter. Learn More about using custom adapters with Next-Auth here.

Installation

Install the package using npm:

$ npm install next-auth-sequelize-adapter --save

If you are using yarn, then type:

$ yarn add next-auth-sequelize-adapter

Usage

Now cd into the db folder, open the models/index.js and replace all code with:

models/index.js

import Sequelize, { Model } from "sequelize";
import {
  userModel,
  accountModel,
  sessionModel,
  verificationRequestModel,
} from "next-auth-sequelize-adapter";

const path = require("path");
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/../config/config.json")[env];
const db = {};

let sequelize;

if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(
    config.database,
    config.username,
    config.password,
    config
  );
}

db.sequelize = sequelize;
db.Sequelize = Sequelize;

db.User = userModel(Model, sequelize, Sequelize);
db.Account = accountModel(Model, sequelize, Sequelize);
db.Session = sessionModel(Model, sequelize, Sequelize);
db.VerificationRequest = verificationRequestModel(Model, sequelize, Sequelize);

Object.keys(db).forEach((modelName) => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

export default db;

Then edit the pages/api/auth/[...nextauth].js and specify that NextAuth should now use the sequelize adapter.

pages/api/auth/[...nextauth].js

import NextAuth from "next-auth";
import Providers from "next-auth/providers";

// import the local db object and the SequelizeAdapter
import db from "../../../db/models";
import { SequelizeAdapter } from "next-auth-sequelize-adapter";

const options = {
  // specify all the OAuth providers here
  providers: [
    Providers.GitHub({
      clientId: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET,
    }),
  ],

  // specify the database connection string
  database: process.env.DATABASE_CONNECTION_STRING,

  // specify the sequelize adapter and pass the db instance as models
  adapter: SequelizeAdapter.Adapter({ models: db }),
};

export default (req, res) => NextAuth(req, res, options);

Now, these were the minimum requirements to get next-auth working with sequelize ORM, but you might want to extend the default models such as the user model with a phone number field or add a relationship to another model such as posts.

Extending Default Models

To extend a default model, first, generate a new model either manually or with sequelize-cli, then import the relevant schema and spread it inside the init method of your newly generated model. Now you can specify any custom properties such as a phone number.

models/user_extended.js

// import the schema for the model you want to extend
import { userSchema } from "next-auth-sequelize-adapter";

const extendedUserModel = (Model, sequelize, Sequelize) => {
  class User extends Model {
    static associate(models) {
      // define association here
    }
  }
  User.init(
    {
      // spread the schema to get all the default properties
      ...userSchema(Sequelize),

      // define any custom properties you want to extend the model with
      phoneNumber: {
        type: Sequelize.STRING,
        unique: true,
        field: "phone_number",
      },
    },
    {
      sequelize,
      tableName: "users",
      modelName: "User",
    }
  );

  return User;
};

export default extendedUserModel;

Then edit the models/index.js and tell sequelize to use the custom model and not the default model:

models/index.js

import Sequelize, { Model } from "sequelize";
import {
  userModel,
  accountModel,
  sessionModel,
  verificationRequestModel,
} from "next-auth-sequelize-adapter";

// import your extended model
import userModelExtended from "./user_extended";

const path = require("path");
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/../config/config.json")[env];
const db = {};

let sequelize;

if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(
    config.database,
    config.username,
    config.password,
    config
  );
}

db.sequelize = sequelize;
db.Sequelize = Sequelize;

// Comment out or remove the default model
// db.User = userModel(Model, sequelize, Sequelize);

// Initialize the extended model and use it in your project
db.User = userModelExtended(Model, sequelize, Sequelize);
db.Account = accountModel(Model, sequelize, Sequelize);
db.Session = sessionModel(Model, sequelize, Sequelize);
db.VerificationRequest = verificationRequestModel(Model, sequelize, Sequelize);

Object.keys(db).forEach((modelName) => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

export default db;

Creating New Models

Creating models other than the default next-auth models is very simple. Just generate the new model and its migration using the sequelize-cli and include it in the models/index.js file using the ES6 import syntax.

Extending The Session Object

To extend the session object and include custom properties like the phone number of a user, check out the next-auth documentation here

Contributing

Contributions make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.