I'm using LocalStrategy with MYSQL(with sequelize) which is working except when MYSQL throws exception (just to test, I shut down MYSQL server). The return done(error)
callback throwing exception crashing server.
Here is my piece of code:
passport.use(new LocalStrategy({usernameField: 'email', passwordField: 'password'},
function (email, password, done) {
db.User.find({where: {email: email}}).done(function (error, user) {
if(error) return done(error);
if (!user) return done(null, false, {message: 'unknown user'});
//validate password
if (user.password != password) {
return done(null, false, {message: 'invalid password'});
}
//all ok
return done(null, user);
});
}
));
And the exception:
TypeError: Property 'next' of object #<Context> is not a function
at Context.actions.error
What am I doing wrong? Thanks!
EDIT:
req._passport.instance.authenticate('local', function (err, user, info) {
if (err) return validator.emit('exception', err);
if (!user) {
validator.result.errors.push('Username and password combination not found.');
validator.emit('response');
} else {
req.login(user, function (error) {
if (error) return validator.emit('exception', error);
validator.emit('response');
});
}
})(req, res);
Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.
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.
OK silly me, I missed the next
at the end
req._passport.instance.authenticate('local', function (err, user, info) {
.....
})(req, res, next);
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