最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Understanding Passportjs Custom Callback - Stack Overflow

programmeradmin24浏览0评论

I'm experimenting with Passportjs and the code for a Custom Callback is:

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);
});

I'm happy with all of this code except for the second to last line (req, res, next); - Could someone explain why these parameters are added on the end. This is probably more of a JS question than a Passport question but any help is much appreciated.

I'm experimenting with Passportjs and the code for a Custom Callback is:

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);
});

I'm happy with all of this code except for the second to last line (req, res, next); - Could someone explain why these parameters are added on the end. This is probably more of a JS question than a Passport question but any help is much appreciated.

Share Improve this question edited Jan 7, 2015 at 8:27 Martijn Pieters 1.1m320 gold badges4.2k silver badges3.4k bronze badges asked May 7, 2014 at 16:29 tommyd456tommyd456 10.7k28 gold badges94 silver badges165 bronze badges 2
  • 1 It's a passport question as well. Apparently, passport.authenticate(…) does return a function. – Bergi Commented May 7, 2014 at 16:38
  • 1 Looks like it's designed to be written as app.get('/login', passport.authenticate('local', function(err, user, info) {…})); – Bergi Commented May 7, 2014 at 16:39
Add a comment  | 

3 Answers 3

Reset to default 6

The "javascript" answer is that it returns a function which is called again with those 2nd set of arguments.

That function is the "accumulator for failures from each strategy in the chain".

https://github.com/jaredhanson/passport/blob/master/lib/middleware/authenticate.js

You can rewrite it without the anonymous function, or the custom callback. Just use passport's passport.use(new LocalStrategy()) function to create the new strategy. See 'Configure' docs.

passport.use(new LocalStrategy(
  function(username, password, done) {
    logIn({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) {
        return done(null, false, { message: 'Incorrect username.' });
      }
      if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' });
      }
      return done(null, user);
    });
  }
));

The only change you need to make is move the logIn function to be in this file, and not a method to req. Then you can simply call passport.authenticate like so:

app.get('/login', passport.authenticate('local', { successRedirect: '/',
                                                   failureRedirect: '/login' }));

So instead of using res.redirect in the callback, you just use passport's built in successRedirect and failureRedirect properties. You can see their docs as well, on the authentication page.

Connect/Express middleware function has signature:

function(req, res, next)

passport.authenticate() can be used as a middleware, e.g:

app.post('/login', passport.authenticate('local'), nextMiddleware);

This means authenticate() returns a middleware function object, which you can evoke with (req, res, next) parameters to continue the app's request-response cycle.

发布评论

评论列表(0)

  1. 暂无评论