1exports.isLocalAuthenticated = function(req, res, next) {
2 passport.authenticate('local', function(err, user, info) {
3 if (err) { return next(err); } //error exception
4
5 // user will be set to false, if not authenticated
6 if (!user) {
7 res.status(401).json(info); //info contains the error message
8 } else {
9 // if user authenticated maintain the session
10 req.logIn(user, function() {
11 // do whatever here on successful login
12 })
13 }
14 })(req, res, next);
15}
16
1passport.use(new LocalStrategy(
2 function(username, password, done) {
3 User.findOne({ username: username }, function (err, user) {
4 if (err) { return done(err); }
5 if (!user) { return done(null, false); }
6 if (!user.verifyPassword(password)) { return done(null, false); }
7 return done(null, user);
8 });
9 }
10));
11