Error with passportjs: Unknown authentication strategy "local"

node.js, passport.js

Solution

There is a syntax error in this code. `usernameField: 'emailAddress'` Has to be passed in LocalStrategy constructor like this...

passport.use(new LocalStrategy({
   usernameField: 'email'
}, yourAuthenticateFunction));

Problem

I am developing a nodejs application and using passportjs for authentication. I am using local strategy of passport. But when I try to login, I am getting following error: ``` Error: Unknown authentication strategy "local" at attempt (/home/project/node_modules/passport/lib/middleware/authenticate.js:166:37) at authenticate (/home/project/node_modules/passport/lib/middleware/authenticate.js:338:7) at exports.authenticate (/home/project/controllers/RegistrationsController.js:87:4) at callbacks (/home/project/node_modules/express/lib/router/index.js:164:37) at param (/home/project/node_modules/express/lib/router/index.js:138:11) at pass (/home/project/node_modules/express/lib/router/index.js:145:5) at Router._dispatch (/home/project/node_modules/express/lib/router/index.js:173:5) at Object.router (/home/project/node_modules/express/lib/router/index.js:33:10) at next (/home/project/node_modules/express/node_modules/connect/lib/proto.js:193:15) at /home/project/node_modules/express-flash/lib/express-flash.js:31:7 ``` Here is my passport-config.js file: ``` var passport = require('passport'); var LocalStrategy = require('passport-local').Strategy; var User = require('../models/user'); passport.serializeUser(function(user, done) { done(null, user.id); }); passport.deserializeUser(function(id, done) { User.findById(id, function (err, user) { done(err, user); }); }); passport.use({usernameField: 'emailAddress'}, new LocalStrategy(function(username, password, done) { User.findOne({ emailAddress: username }, function(err, user) { if(err){ return done(err); } if (!user) { return done(null, false, { message: 'Email ' + username + ' not found'}); } else{ //check if password matches and pass parameters in done accordingly } }); })); ``` And following is my RegistrationsController.js file, where my authenticate api resides, ``` var passport = require('passport'); exports.authenticate = function(req, res, next){ console.log('Login request!'); passport.authenticate('local', function(err, user, info) { console.log('In authenticate callback!'); if (err) return next(err); if (!user) { req.flash('errors', { msg: info.message }); res.status(500).json({message: info.message}); } res.json(user); })(req, res, next); } ``` Have been looking at the code since past 2 days but could not figure out the error yet. I have installed both `passport` and `passport-local` modules. Any help would be greatly appreciated.

Original source