passport.js serializeUser done (cannot understand logic of how it works)

Viewed 19

Code: (from The Net Ninja: https://www.youtube.com/watch?v=-PuMp5tQ8Jw&list=PL4cUxeGkcC9jdm7QX143aMLAqyM-jTZ2x&index=15&ab_channel=TheNetNinja)

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

//...
// new user created
done(null, newUser);

Questions:

  1. how “serializeuser” method knows about user object? it doesn’t have access to it? how does it access to user?
  2. is “serializeUser method some kinda event listener? whenever done is called, it will do something.
  3. why don’t we directly call done(null, user.id) after a new user is created.
  4. how done(null, newUser) triggers serializeUser method? I cannot understand it.

Note: I know how callback functions work, but this one is different. I cannot understand how serialize function gets called after done function is called. And I cannot understand how serialize function can access to user? I've searched but cannot find explanation.

0 Answers
Related