【发布时间】:2018-08-04 15:36:56
【问题描述】:
我对使用 JWT 和令牌完全陌生;我只是尝试这样做:
console.log("logged " + data);
//prints { _id: 5a82ee98e918b22e83d6c3e0,
//username: 'test2',
//name: 'test',
//surname: '2',
//password: '...'
//[etc]
//}
console.log("logged _id " + data._id);
//prints 5a82ee98e918b22e83d6c3e0
var token = jwt.sign(data._id, secret, {
expiresIn: "24h" // expires in 24 hours
});
console.log("saved token " + token);
//prints the token in style eyJhbGciOi[...].eyJfYnNvbn[...].usoLRz-[...]
console.log("decoded: " + JSON.stringify( jwt.decode(token) ) )
//prints the token in this way:
//{"_bsontype":"ObjectID","id":{"type":"Buffer","data":[90,130,238,152,233,24,178,46,131,214,195,224]},"iat":1519502719,"exp":1519589119}
- 为什么不以纯文本形式打印身份证号?
- 如何获取我放入令牌的 ID 号?
更新: 我在登录功能; data 是登录后的答案,包含登录的用户;我有
var login = function(req, res) {
passport.authenticate('local', function(err, data, info) {
if (err) { console.log(err);
}
console.log("data in auth " + data);
if (!data) {
return res.status(404);
}
req.logIn(data, function(err) {
if (err) {
console.log("err " + err);
}
console.log("logged " + data);
console.log("logged _id " + data._id);
var token = jwt.sign[continues on top]
[...]
}
【问题讨论】:
-
在签署之前如何将声明分配给 jwt?这似乎是相关的。
-
我在登录功能; data 是登录后的答案,它包含登录的用户;我在顶帖上添加了代码
标签: javascript jwt token