Registry / security / oauth2-server-mongoose

oauth2-server-mongoose

JSON →
library1.3.3jsnpmunverified

MongoDB/Mongoose storage backend for the oauth2-server library. Stable v1.3.3, low release cadence. Integrates with oauth2-server to store OAuth clients and users in MongoDB using Mongoose models. Uses the mongoose-bcrypt plugin to encrypt client secrets and user passwords. Differentiators: minimal configuration, bundled CLI scripts for creating clients and users. Requires Node >=10, mongoose >=5, and mongoose-bcrypt >=1.5 as peer dependencies.

npm install oauth2-server-mongoose
INSTALL
IMPORT
SIG · OAUTH2-SERVER-MONG
O
oauth2-server-mongoose
securityjavascriptv1.3.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import mongooseStore from 'oauth2-server-mongoose'
const mongooseStore = require('oauth2-server-mongoose').default
CommonJS uses require without .default
default
const mongooseStore = require('oauth2-server-mongoose')
const mongooseStore = require('oauth2-server-mongoose').default
CommonJS default import
OAuth2Server
import OAuth2Server from '@compwright/oauth2-server'
const OAuth2Server = require('@compwright/oauth2-server')
CommonJS require is also correct, but ESM is preferred

Shows how to initialize OAuth2Server with mongooseStore and perform password grant.

const OAuth2Server = require('@compwright/oauth2-server'); const mongoose = require('mongoose'); const mongooseStore = require('oauth2-server-mongoose'); mongoose.connect('mongodb://localhost/oauth2'); const oauth = new OAuth2Server({ model: { ...mongooseStore({ // Optional custom models }) } }); // Example: grant password credentials type const token = await oauth.token({ body: { grant_type: 'password', username: 'user', password: 'pass' }, headers: { authorization: 'Basic ' + Buffer.from('clientId:clientSecret').toString('base64') } }); console.log(token.accessToken);
Debug
Known issues
gotchaThe package requires peer dependencies mongoose and mongoose-bcrypt to be installed separately. Missing them will cause runtime errors.
fix
Install all peer deps: npm install mongoose mongoose-bcrypt
affects: >=1.0
deprecatedThe package uses mongoose-bcrypt which may not support Mongoose 6+ due to plugin API changes.
fix
Use Mongoose 5, or check compatibility with mongoose-bcrypt latest version
affects: >=5.0 <6.0
gotchaCustom models must retain the same plugins (mongoose-bcrypt) and static methods. Omitting them will break password/secrets encryption.
fix
Ensure custom models include the mongoose-bcrypt plugin and implement required methods like authenticateUser.
affects: >=1.0
gotchaThe package expects MongoDB connection to be already established. Calling mongooseStore before connecting will fail.
fix
Call mongoose.connect() before initializing oauth2-server-mongoose.
affects: >=1.0
Errors
Common errors & fixes
Error: OAuth2Server requires a model with getAccessToken, saveAccessToken, ...
Missing or incomplete model object passed to OAuth2Server.
fix
Ensure you spread mongooseStore() into the model property: model: { ...mongooseStore() }
ValidationError: Client secret must be encrypted
Client model not using mongoose-bcrypt or custom model missing encryption.
fix
Include mongoose-bcrypt plugin in your client model schema.
MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
MongoDB connection not established or auth credentials wrong.
fix
Verify mongoose.connect() is called before any model operations.
TypeError: mongooseStore is not a function
Incorrect import of default export in ES module environment.
fix
Use `const mongooseStore = require('oauth2-server-mongoose');` (no .default) or import correctly.
Upgrade
Version history
1.3.3latest on npm
Audit
Dependencies
@compwright/oauth2-serverrequiredcore OAuth2 server library that this storage backend works with
mongooserequiredMongoDB ODM for database models and connection
mongoose-bcryptrequiredplugin for encrypting client secrets and user passwords
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
oauth2-server-mongoose — npm install oauth2-server-mongoose · libregistry