Update logged in user details in session

前端 未结 3 804
时光取名叫无心
时光取名叫无心 2020-12-30 23:35

I am using PassportJS with ExpressJS.

I need to update the logged in user details. While I do update this in the DB, how do I update it in the session too so that re

3条回答
  •  死守一世寂寞
    2020-12-30 23:59

    I had similar problem today and decided to share my findings, since i couldn't find similar answer.

    The problem was that (copied from passport documentation) i was getting the user data directly from the token, that the user sent in the request. Which was of course outdated.

    passport.use(new JWTStrategy({
        jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
        secretOrKey   : CONFIG.JWT_SECRET
      },
      function (jwtPayload, cb) {
        return cb(null, jwtPayload);
      }
    ));
    

    while i should get the fresh user object from the database instead:

    return User.findById(jwtPayload.id)
      .then(user => {
        return cb(null, user);
      })
      .catch(err => {
        return cb(err);
      });
    

提交回复
热议问题