title: PassportJS serializeUser and deserializeUser execution flow
body: <p>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.</p>
  
  <p>I understand it is used to save the user information in session for persistent login. My code for serialization and deserialization is</p>
  
  <pre><code>passport.serializeUser(function(user, done){
      done(null, user.id);
  });
  
  passport.deserializeUser(function(id, done){
      User.findById(id, function(err, user){
          done(err, user);
      });
  });
  </code></pre>
  
  <p>My question regarding this</p>
  
  <p>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 </p>
  
  <pre><code> passport.serializeUser(function(user, done){
      console.log(arguments.callee.caller);
      done(null, user.id);
  });
  </code></pre>
  
  <p>And got <strong>[Function: pass]</strong> in output
  Can anyone explain this?</p>
  
  <p>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</p>
  
  <pre><code>{ _id: 5505f231b810dbd4098ac76a,
    __v: 0,
    google: {},
    twitter: {},
    facebook: {},
    local:
     { password: '$2a$08$9NGd0xNu0JbWMZ07ufyFRu8guwy147k8IBl5cAC4Y8APOuxreNI32',
       email: 'xxxx@xxx.com' } }
  </code></pre>
  
  <p>How is this possible?</p>
  
  <p>3) Where the control flow execution goes once <code>done</code> method is executed? </p>