Okay, so I am just getting in to the MEAN stack, and I'm trying to build an app with Passport.js.
I'm just starting user serialization to maintain sessions. In their example, Passport uses this for serialization and deserialization:
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
So, my question is this: is this example considered secure? If I understand this right, doesn't that mean that a client could just fake the user ID to become logged in as whichever user has that ID?
I guess what I'm asking is, is their example considered "secure" and a proper way of doing things, or is it expected that you will change these functions to generate unique serialization. If this is considered secure, then I think I'm missing something on how this works, and I'd love to be filled in.
On the other hand, if this is not secure and I am expected to write my own functions in place of these, would the following be a valid and secure way of doing this:
- Upon serialization of a user, generate a random hash and put that in the user's database entry. Random hash is the serial that represents that user.
- Upon deserialization, look up the random hash in the database and return the corresponding user. If the hash isn't found throw some kind of error.
- When the user logs out, delete their serial hash from their entry in the database.
If my logic up until here is valid, what would be a proper way to generate this random hash?