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.
Passport is authentication middleware for Node.js. Extremely flexible and modular, Passport can be unobtrusively dropped in to any Express-based web application. A comprehensive set of strategies support authentication using a username and password, Facebook, Twitter, and more.
In this route, passport. authenticate() is middleware which will authenticate the request. By default, when authentication succeeds, the req. user property is set to the authenticated user, a login session is established, and the next function in the stack is called.
This module lets you authenticate using a username and password in your Node. js applications. By plugging into Passport, local authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With