You should take a look at the Custom Callback section in your passport documents , which explains how to override the built-in authentication request processing behavior. You can write a custom callback that will serve as a server for the done function that you call from Strategy.
app.get('/login', function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (err) { return next(err); } if (!user) { return res.redirect('/login'); } req.logIn(user, function(err) { if (err) { return next(err); } return res.redirect('/users/' + user.username); }); })(req, res, next); });
Look at the second parameter for the passport.authenticate call, which will execute the function that you are calling from the local strategy.
See the done function called in the code below, which is determined by the local strategy for the passport. You can call the executed function with various available parameters, such as err , user , info , set from the strategy in accordance with the response to the API call or the db operation. these parameters will be processed by the function definition described above in the passport.authenticate call.
passport.use(new LocalStrategy( function(username, password, done) { User.findOne({ username: username }, function (err, user) { if (err) { return done(err); } if (!user) { return done(null, false); } if (!user.verifyPassword(password)) { return done(null, false); } return done(null, user); }); } ));
source share