Do I implement serialize and deserialize NodesJS + Passport + RedisStore?

后端 未结 3 1642
自闭症患者
自闭症患者 2021-01-30 02:07

Do I implement Serialize and Deserialize?

RedisStore is setup as my session store with Express. Does this mean that I DO NOT implement Serialize and Deserialize? Will i

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 02:21

    Given the following configuration of express-session with connect-redis as the session store (using Express 4):

    redis = require('redis').createClient(6379, '127.0.0.1');
    session = require('express-session');
    RedisStore = require('connect-redis')(session);
    
    app.use(session({
      store: new RedisStore({
        client: redis
      }),
      secret: 's3cret',
      resave: true,
      saveUninitialized: true
    }));
    

    You can just tell passport to serialize the entire user object, instead of just the user id.

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

    The entire user object will be saved with the session in Redis, and placed on the request as req.user for every request.

提交回复
热议问题