"Error: failed to deserialize user out of session"
I am getting "Error: failed to deserialize user out of session" - the problem is that this generates 500 server error and I can't present a nice page to the user to say "please log in again". How can I work around this?
The issue is related to https://github.com/LaunchAcademy/generator-engage/issues/122 and https://github.com/jaredhanson/passport/issues/6 - what happens is that I have a bad cookie with User ID that does not exist. In deserializeUser my function User.findOne correctly returns an error and I pass this to Passport's done/callback with false as others have done before me. But Passport still creates a 500 error instead of redirecting back to the login page. I guess I could do some sort of a hack and try to clear the cookie in the deserializeUser but feels hacky.
What is the way to solve this? Is there a way to instruct Passport to forward the request back to the login page in case of an error in deserializeUser rather than generate server error 500?? It's not that 500 is ugly but it also prevents user from logging on until the cookie is cleared manually.
My code below for reference
passport.deserializeUser(function(user, cb) {
process.nextTick(async function() {
var usr = false;
usr = await User.findOne(null, user.id)
.catch(err =>
cb(err, false)); // this gets triggered by User.findOne as the user.id is non-existent in the DB
return cb(null, usr || false);
});
});
I have tried returning cb(err) or cb(err, null) etc but none worked
Don't use user.id. Just use user. So your
usr = await User.findOne(null, user.id)
code should be
usr = await User.findOne(null, user)