简体   繁体   English

哪个数据放在jwt-tokens(node.js + mongoose)中?

[英]Which data put in jwt-tokens (node.js + mongoose)?

Sorry for bad English. 抱歉英语不好。 To generate JWT-token i'm using 生成我正在使用的JWT令牌

jwt     = require('jsonwebtoken');
/* some code */

//function to create jwt-token
function createToken(user) {
  return jwt.sign(user, config.secret, { expiresIn: 60*60*5 });
}

/* some code */

//after registration/authorization (if success) createToken token function is calling
createToken(user);

I would like to learn the best practices, what data must be passed to a function to create the token. 我想学习最佳实践,必须将哪些数据传递给函数来创建令牌。 For example, can this be as: 例如,这可以是:
login (John) and id( ObjectId("5821d94dbb021a1360582da3") when using MongoDb)?
And here, I think, will be relevant question: 在这里,我认为,这将是相关的问题:
If I store in token some information that allows initialize user, I can pull its data from the database. 如果我在令牌中存储了一些允许初始化用户的信息,我可以从数据库中提取数据。 Is this correct, initialize the user from authorization header from JWT? 这是正确的,从JWT的授权标头初始化用户? For initialization I'm using express-jwt , which, if successful, sets req.user ? 对于初始化我正在使用express-jwt ,如果成功,则设置req.user Thanks. 谢谢。

you can put the generated token in browser local storage, if you are using browser. 如果您使用的是浏览器,则可以将生成的令牌放入浏览器本地存储中。 You can also store session values in the token itself, those values will be decoded on the server side by nodejs. 您还可以在令牌本身中存储会话值,这些值将在服务器端由nodejs解码。

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

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