Passport logout throws after upgrading to 0.6 or newer
Error: req#logout requires a callback functionShort 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.
Fix it fast
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(" src2Match what you see, then jump to its fix
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" src2Fix
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/logoutConfirmed by passportjs.org
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" src2Fix
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" srcConfirmed by github.com
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
Back end
Passport logs the user in but req.isAuthenticated() returns false on the next request
req.user is undefined on the next request
req.isAuthenticated() only checks that req.user is set.
4 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fix