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

Viewed 83

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("/")
})
2 Answers

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('/');
  });
});
Related