In am following a simple MVC tutorial https://blog.logrocket.com/building-structuring-node-js-mvc-application/ to build a login authentication system.
Till a few days back, I had a working serialize and deserialize section in my code
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
User.findById(id, (error, user) => {
done(error, user);
});
});
Recently running the code, I started getting the error Model.findById() no longer accepts a callback
Now I understand that the new way would be using write async and await. However, I couldn't find a sample online that guides me towards how to come about writing the code. Could I please be helped here?
Your question isn't so much a Passport question, but a Mongoose question, which now uses promises instead of callbacks.
To convert your code, you can handle the promise using async/await
, but you still need to call the callback provided by passport.deserializeUser
:
passport.deserializeUser(async (id, done) => {
try {
return done(null, await User.findById(id));
} catch(error) {
return done(error);
}
});