1

我在我的 MEAN Stack webapp 中使用 Satellizer 来登录用户。satellizer 模块使用 JSON Web 令牌。

令牌创建于:

var jwt = require('jwt-simple');

function createJWT(user) {
  var payload = {
    sub: user._id,
    user: {
            displayName: user.displayName,
            email: user.email,
            admin: user.admin
        },
    iat: moment().unix(),
    exp: moment().add(2, 'hours').unix()
  };
  return jwt.encode(payload, config.TOKEN_SECRET);
}

app.post('/auth/login', function(req, res) {
  User.findOne({ email: req.body.email }, '+password', function(err, user) {
    if (!user) {
      return res.status(401).send({ message: 'Wrong email and/or password' });
    }
    user.comparePassword(req.body.password, function(err, isMatch) {
      if (!isMatch) {
        return res.status(401).send({ message: 'Wrong email and/or password' });
      }
      res.send({ token: createJWT(user) });
    });
  });
});

问题是稍后在函数中,我需要更新有效负载对象中的用户密钥。

这可能吗?

4

1 回答 1

0

基本上令牌看起来像字符串。当您更改有效负载时,您的令牌会更改(新字符串)。您不能在不更改字符串的情况下更改令牌/有效负载。您可以在以前的基础上创建新的。

请记住将新令牌返回给客户端应用程序。

于 2016-02-17T21:20:03.780 回答