我能够创建一个 jwt 令牌:
fastify.post('/signup', (req, reply) => {
const token = fastify.jwt.sign({
payload,
})
reply.send({ token })
})
可以返回如下内容:
{“令牌”:“eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MjM3MDgyMzF9.HZqqiL7wwPaEQihUGoF7Y42Ia67HgKJ-1Ms38Nvcsmw”}
但是如果我尝试从令牌中解码用户名
fastify.get('/decode', async (request, reply) => {
const auth = request.headers.authorization;
const token = auth.split(' ')[1]
fastify.jwt.verify(token, (err, decoded) => {
if (err) fastify.log.error(err)
fastify.log.info('username : ' + decoded.username)
reply.send({
foo: decoded,
})
})
})
回应是:
{"foo":{"iat":1523660987}}