Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Passport logout throws after upgrading to 0.6 or newer

ErrorError: req#logout requires a callback function

Short answer

Since Passport 0.6, logout is asynchronous because it regenerates the session. It needs a callback as its last argument. Pass a function, handle its error, and redirect inside it. Use a POST route for the logout link, not a plain GET.

  • Applies to Passport 0.6, Express 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every place that calls logout and check the Passport version that npm actually installed.

    npm ls passport
    grep -rn "\.logout(" src
  2. 2Match what you see, then jump to its fix

    1. 1The call looks like req.logout(); followed by res.redirect('/')req.logout() is called without a callbackFix
    2. 2Values such as a cart or a return URL vanish right after req.login or req.logoutSession data is gone after login or logoutFix

What does the error look like?

  • Error: req#logout requires a callback function

Cause 1 of 2

req.logout() is called without a callback

1Check

The changelog for 0.6.0 states that logout is now an asynchronous function and requires a callback function as the last argument. Older code and old tutorials do not pass one.

grep -rn -A2 "req.logout" src

2Fix

Move the redirect into the callback and pass the error to next.

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

3Verify

Log in, then post to the route and confirm you are signed out.

curl -i -X POST -b cookies.txt http://localhost:3000/logout

Confirmed by passportjs.org

Did this fix it?

Cause 2 of 2

Session data is gone after login or logout

1Check

From 0.6.0, req.login and req.logout regenerate the session and clear session information by default.

grep -rn "req.session" src

2Fix

Read what you need before the call and store it again afterwards, or pass keepSessionInfo: true to keep the old data.

req.logout({ keepSessionInfo: true }, function (err) {
  if (err) { return next(err); }
  res.redirect('/');
});

3Verify

Set a session value, log out, and read the value in the next request.

grep -rn "keepSessionInfo" src

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Pin the Passport version in package.json and read the changelog before a minor upgrade of 0.x packages.
  • Use POST or DELETE for logout, as the Passport docs advise,.
  • Add one test that logs in and out through the real route.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.