passportjs serializeuser 1

passportjs serializeuser

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function(err, user) {
    done(err, user);
  });
});

Here is what the above code is Doing:
1. We are importing the passport module and the local strategy module.
2. We are importing the User model.
3. We are creating a new local strategy using the local strategy module.
4. We are using the User model to find a user with the same email as the one the user is trying to login with.
5. If the user is found, we are checking to see if the password the user entered is the same as the one stored in the database.
6. If the password is correct, we are calling the done callback function with the user object.
7. If the password is incorrect, we are calling the done callback function with false.
8. We are telling passport to use the local strategy we defined.
9. We are serializing the user object to the session.
10. We are deserializing the user object from the session.

Similar Posts