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

v1.0.3

Published

Next-Auth adapter for Sequelize ORM.

Downloads

19

Readme

Sequelize Adapter For Next-Auth

GitHub issues GitHub forks GitHub stars

Getting Started

To get stated simply clone the example or use the sequelize-cli and generate the folder structure for Sequelize ORM.

Prerequisites

You must install sequelize in your NextJS/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

After generating the sequelize folder structure with the $ sequelize init command, edit the models/index.js and replace all code with:

models/index.js 

'use strict';

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 the sequelize adapter instead of the default TypeORM adapter.

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

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

// import the local db object and the Adapter
import db from '../../../db/models';
import { Adapter } 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 that next-auth should use the sequelize adapter instead of the default TypeORM adapter and pass the db instance as models
  adapter: Adapter.Adapter({ models: db })
};

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

Now this is enough to get next-auth working with sequelize ORM but that is not always the case and the default models, most likely the user model needs to be extended with fields such as a phone number or with a relationship to another table such as posts.

Extending Default Models

To extend a default model, you first need to create a new model either manually or with sequelize-cli, then edit it so that we can have all the default properties such as name, email, and image, which are required by next-auth, along with custom properties such as a phone number.

models/user_extended.js

'use strict';

// import the schema for the model you want to extend
import { 
  userSchema, accountSchema, sessionSchema, verificationRequestSchema 
} from 'next-auth-sequelize-adapter';

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

    // define any custom properties you want the model to be extended 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 so that next-auth uses the extended model instead of the default model:

models/index.js 

'use strict';

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 user model
// db.User = userModel(Model, sequelize, Sequelize);

// Initialize the extended model instead and use it normally 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 create the model and the migration with sequelize-cli and include it in the models/index.js file using the ES6 import syntax and use it normally in your project.

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.