node.js - Why do I have to serialize user object properties with PassportJS for sessions? -


could explain in detail, or @ least reference explanation of how works. why have serialize (byte stream) user.id or prop of user object stored cookie on client?

    passport.serializeuser(function (user, done) {         done(null, user.id);     });      passport.deserializeuser(function (id, done) {         connection.query("select * users id = ?", [id], function (err, rows) {             //verify callback invokes .done()             done(err, rows[0]);         });     }); 

you don't want store whole user object in ram when may have 100's or more logged in users, want remember minimal info able find info later if/when need it. serialize() how decide keep in ram , deserialize() how discarded data later, when need it.

i have commented original code illustrate.

// minimal info can record user later? passport.serializeuser(function (user, done) {      done(null, user.id); // user id enough. let's go that. });  // user came , making request. have lousy id. how rest of data need? passport.deserializeuser(function (id, done) {      // let's them in database.     connection.query("select * users id = ?", [id], function (err, rows) {         //verify callback invokes .done()         done(err, rows[0]);      }); }); 

all of above sets , reads res.session. how sessions saved or matched cookies unrelated. express-session common library there others.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -