Search code examples
node.jsexpresspassport.jsnode.js-connect

Passportjs - req.logout function does not exist


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?


Solution

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

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