Passportjs - req.logout function does not exist

早过忘川 提交于 2021-01-29 21:55:35

问题


I have a route that looks like so:

  exports.logout = function(res, req){
      req.logout() // I blow up
      res.redirect('/')
    }

Error: Object #ServerResponse has no method 'logout'

The Request object does not contain a logout function when this route is called. I assume that this is because I have my middleware in the wrong order. Is that correct? This is what my configuration looks like:

app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser('meow'));
app.use(express.bodyParser());
app.use(express.session());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.logger('dev'));
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

Are these passport middleware caveats documented anywhere? The project README gives an example and I have those middleware included in that order but I suspect that another middleware set or use is breaking me. Can anyone shed some light on this for me?


回答1:


You accidentally switched your res and req formal parameters. Should be:

exports.logout = function(req, res){
  req.logout();
  res.redirect('/');
}



回答2:


Looks like you switched req and res.

Error: Object #ServerResponse has no method 'logout'

ServerResponse is res not req.



来源:https://stackoverflow.com/questions/19532346/passportjs-req-logout-function-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!