【问题标题】:JWT updating payload from node.jsJWT 从 node.js 更新有效负载
【发布时间】:2016-05-29 17:54:51
【问题描述】:

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

令牌创建于:

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) });
    });
  });
});

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

这可能吗?

【问题讨论】:

    标签: javascript angularjs node.js jwt satellizer


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 2019-01-16
      • 2016-03-12
      • 2020-09-18
      • 2019-10-29
      • 2020-11-15
      • 2019-10-14
      • 2018-08-06
      • 2015-10-30
      相关资源
      最近更新 更多