JWT 从节点.js更新有效负载

JWT updating payload from node.js

本文关键字:有效 负载 更新 js 节点 JWT      更新时间:2023-09-26

我在我的MEAN Stack网络应用程序中使用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) });
    });
  });
});

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

这可能吗?

基本上令牌看起来像字符串。 当您更改有效负载时,您的令牌也会更改(新字符串)。如果不更改字符串,则无法更改令牌/有效负载。您可以基于以前的创建一个新。

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