I am using Satellizer in my MEAN Stack webapp to login users. The satellizer module uses JSON Web Tokens.
The token is created in:
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) });
});
});
});
The thing is that later in a function, I need to update the user key inside the payload object.
Is this possible?
Basically token looks like string. when you change payload then your token is changed (new string). You can't change token / payload without changing string. You can create new one based on previous.
Remember to return new token to client application.