当然,这取决于您实际提供数据的方式
.sign()
首先,简单地提供
ObjectID
或
而是提供值。
general usage for
.sign()
如果负载不是缓冲区或字符串,则将使用JSON.stringify将其强制为字符串。
简而言之,该版本将“字符串化”对象形式,需要一些“挖掘”。在您的形式中,解码对象具有以下属性:
id
子属性为
data
其中包含可转换为
Buffer
.这就是你要做的:
let newId = Buffer.from(req.decoded.id.data).toString('hex');
和
newId
'hex'
ObjectId
_id
.
.符号()
.toString()
来自
ObjectId
.verify()
是否只是提供的“十六进制字符串”,而不是
JSON.stringify
上的结果
ObjectID
它本身
用列表演示:
const bson = require('bson'),
jwt = require('jsonwebtoken');
// Stored ObjectID
console.log("Round 1");
(function() {
let id = new bson.ObjectID();
console.log("Created: %s", id);
let token = jwt.sign(id,'shhh'); // Supply value as ObjectID
let decoded = jwt.verify(token,'shhh');
console.log("Interim");
console.log(decoded);
let newId = Buffer.from(decoded.id.data).toString('hex');
console.log("Decoded: %s", newId);
})();
console.log("\nRound 2");
// Stored String value
(function() {
let id = new bson.ObjectID();
console.log("Created: %s", id);
let token = jwt.sign(id.toString(), 'shhh'); // Supply value as string
let decoded = jwt.verify(token,'shhh');
console.log("Decoded: %s", decoded);
})();
给出输出,显示输入值和解码值:
Round 1
Created: 59857328090c497ce787d087
Interim
{ _bsontype: 'ObjectID',
id:
{ type: 'Buffer',
data: [ 89, 133, 115, 40, 9, 12, 73, 124, 231, 135, 208, 135 ] },
iat: 1501917992 }
Decoded: 59857328090c497ce787d087
Round 2
Created: 59857328090c497ce787d088
Decoded: 59857328090c497ce787d088
.符号()
.验证()