简体   繁体   中英

Passport.js - store information in session

I currently have a mongoose/express/passportjs application that I can successfully log in and out with. However, only the user ID is stored in the session. I'd like the store the whole user object.

Here is the code I use:

passport.serializeUser(function(user, done) {
  done(null, user._id);
});

passport.deserializeUser(function(_id, done) {
  done(null, { _id: _id });
});

Works great, here is why I tried to serialize the entire user:

passport.serializeUser(function(user, done) {
  done(null, user._id);
});

passport.deserializeUser(function(user, done) {
  done(null, user);
});

However, it doesn't store any session with that.

What am I doing wrong?

In serializeUser you need to pass in the entire user to done :

passport.serializeUser(function(user, done) {
  done(null, user);
});

passport.deserializeUser(function(user, done) {
  done(null, user);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM