user17306349
user17306349

Reputation:

why am i getting this error when i try to req.logout()?

Error which i am getting:

Error: req#logout requires a callback function

My code:

// @desc Logout User
// @rote GET /auth/logout
router.get("/logout", (req, res)=>{
    req.logout()
    res.redirect("/")
})

Upvotes: 2

Views: 1186

Answers (2)

Passionate Coder
Passionate Coder

Reputation: 7294

This is a change introduced in recent release. You can find it here in detail https://medium.com/passportjs/fixing-session-fixation-b2b68619c51d

From the documentation https://www.passportjs.org/concepts/authentication/logout/

try below code

router.get("/logout", function(req, res, next) {
  req.logout(function(err) {
    if (err) {
      return next(err);
    }
    res.redirect("/");
  });
});

Upvotes: 0

Dennis Kozevnikoff
Dennis Kozevnikoff

Reputation: 2277

req.logout() is an asynchronous function (it was not this way before, they only introduced this change recently for security reasons), and your current code is synchronous, which is why you get this error.

You can fix this error by modofying your code as follows:

app.post('/logout', function(req, res, next) {
    req.logout(function(err) {
    if (err) { return next(err); }
    res.redirect('/');
  });
});

Upvotes: 1

Related Questions