I'm using passportJS with express to authenticate user by local strategy. I have seen few articles regarding how passport is setup and the execution flow. Although most of the thing regarding passport can be figured out by searching, there is serialization and deserialization of user which keeps me confused.
I understand it is used to save the user information in session for persistent login. My code for serialization and deserialization is
passport.serializeUser(function(user, done){
done(null, user.id);
});
passport.deserializeUser(function(id, done){
User.findById(id, function(err, user){
done(err, user);
});
});
My question regarding this
1) Who calls and populates the arguments of the serializeUser and deserializeUser? And how it has access to the user object? To understand this I added log like
passport.serializeUser(function(user, done){
console.log(arguments.callee.caller);
done(null, user.id);
});
And got [Function: pass] in output Can anyone explain this?
2) I am using mongodb to store the user information. MongoDB has _id as the default id of document. So ideally the serializeUser and deserializeUser should have worked with user._id instead of user.id. But it is working fine with user.id which is not available in User the object. Here is the user object printed in console
{ _id: 5505f231b810dbd4098ac76a,
__v: 0,
google: {},
twitter: {},
facebook: {},
local:
{ password: '$2a$08$9NGd0xNu0JbWMZ07ufyFRu8guwy147k8IBl5cAC4Y8APOuxreNI32',
email: 'xxxx@xxx.com' } }
How is this possible?
3) Where the control flow execution goes once done
method is executed?