繁体   English   中英

JWT从node.js更新有效负载

[英]JWT updating payload from node.js

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

关键是,在以后的功能中,我需要更新有效负载对象内的用户密钥。

这可能吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM