Registry / security / passport-local-mysql

passport-local-mysql

JSON →
library5.0.10jsnpmunverified

A Mongoose plugin that integrates with Passport and passport-local to provide username and password authentication for Node.js applications. Version 5.0.10 (stable) is actively maintained with regular releases. Key differentiators: adds hash/salt fields automatically, provides static authenticate/serialize/deserialize methods, and supports customizable username fields and password validators. Unlike manual passport-local setup, it manages password hashing and session serialization automatically.

npm install passport-local-mysql
INSTALL
IMPORT
SIG · PASSPORT-LOCAL-MYS
P
passport-local-mysql
securityjavascriptv5.0.10
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.

passportLocalMongoose
const passportLocalMongoose = require('passport-local-mongoose');
import passportLocalMongoose from 'passport-local-mongoose';
CommonJS by default; ESM not supported in v5
authenticate
User.authenticate()
const { authenticate } = require('passport-local-mongoose');
authenticate is a static method added to the model after plugin
createStrategy
User.createStrategy()
passport.use(User.authenticate());
createStrategy() configures passport-local with correct options; authenticate() only returns a function
serializeUser
User.serializeUser()
passport.serializeUser(User.serializeUser);
Must call the function returned by serializeUser()
deserializeUser
User.deserializeUser()
passport.deserializeUser(User.deserializeUser);
Must call the function returned by deserializeUser()

Sets up Mongoose schema with plugin, configures Passport with local strategy, and creates a user. Demonstrates the minimal setup required.

const mongoose = require('mongoose'); const passport = require('passport'); const LocalStrategy = require('passport-local'); const passportLocalMongoose = require('passport-local-mongoose'); mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true }); const UserSchema = new mongoose.Schema({}); UserSchema.plugin(passportLocalMongoose); const User = mongoose.model('User', UserSchema); passport.use(new LocalStrategy(User.authenticate())); passport.serializeUser(User.serializeUser()); passport.deserializeUser(User.deserializeUser()); const newUser = new User({ username: 'test', password: 'secret' }); newUser.save().then(() => console.log('User saved'));
Debug
Known issues
breakingUpgrading from 1.x to 2.x changes default hash algorithm from SHA1 to SHA256, causing existing passwords to fail.
fix
Either migrate hashes or set options.digestAlgorithm to 'sha1'.
affects: >=2.0.0 <2.0.0
deprecatedThe 'saltlen' option is deprecated in favor of 'saltLength'.
fix
Use 'saltLength' instead of 'saltlen'.
affects: >=4.0.0
gotchacreateStrategy() requires passport and passport-local to be installed.
fix
Ensure both dependencies are in package.json.
affects: >=0.2.1
gotchaPlugin adds 'username' field even if you don't define it in schema.
fix
Define username field explicitly if you need custom options like unique index.
affects: *
breakingMongoose 5.x dropped support for callbacks in favor of promises; plugin methods return promises.
fix
Use async/await or .then() instead of callbacks.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: User.authenticate is not a function
Plugin not properly applied to schema before model creation
fix
Ensure UserSchema.plugin(passportLocalMongoose) is called before mongoose.model('User', UserSchema)
Error: passport-local-mongoose: No username field defined
Schema has no username field and plugin option 'usernameField' not set
fix
Add a username field to schema or set usernameField option in plugin call: UserSchema.plugin(passportLocalMongoose, { usernameField: 'email' })
MongooseError: Model.findOne() is deprecated
Mongoose 6+ deprecates raw findOne usage; plugin uses it internally
fix
Set mongoose.set('strictQuery', false) if using Mongoose 6
Error: Cannot find module 'mongoose'
Mongoose not installed as dependency
fix
Install mongoose: npm install mongoose
TypeError: passport.authenticate is not a function
passport-local not installed or not required
fix
Install passport-local: npm install passport-local
Upgrade
Version history
5.0.10latest on npm
Audit
Dependencies
passportrequiredCore authentication framework
passport-localrequiredLocal strategy implementation
mongooserequiredMongoose schema plugin dependency
Agent activity
12 hits · last 30 days
node
12
Resources
passport-local-mysql — npm install passport-local-mysql · libregistry