Вопрос

I'm referring to the following example:

https://github.com/jaredhanson/passport-local/blob/master/examples/login/app.js

And specifically the following piece of code:

app.post('/login', 
  passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
  function(req, res) {
    res.redirect('/');
  });

The code as posted works just fine. However if I try to refactor it by doing the following:

app.post('/login', 
  authenticate,
  function(req, res) {
    res.redirect('/');
  });

function authenticate() {
    return passport.authenticate('local', { failureRedirect:'/fail', failureFlash:true });
}

things don't work anymore. What am I doing wrong?

Это было полезно?

Решение

I think you'll have to invoke authenticate() (with the parens), in order to execute the function and return the middleware. For example:

app.post('/login', 
  authenticate(),
  function(req, res) {
    res.redirect('/');
  });

The way you currently have it set up, authenticate is installed as a route handler, so express is calling it with req, res arguments (which aren't being used or responded to).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top