Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cant decode token and retrieve username #16

Closed
sensorario opened this issue Apr 14, 2018 · 2 comments
Closed

cant decode token and retrieve username #16

sensorario opened this issue Apr 14, 2018 · 2 comments

Comments

@sensorario
Copy link

As reported here:

https://stackoverflow.com/questions/49831398/retrieve-username-from-jwt-token-using-fastify

I am able to create a jwt token:

fastify.post('/signup', (req, reply) => {
  const token = fastify.jwt.sign({
    payload,
  })
  reply.send({ token })
})

that can return something like:

{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MjM3MDgyMzF9.HZqqiL7wwPaEQihUGoF7Y42Ia67HgKJ-1Ms38Nvcsmw"}

but if I try to decode the username frmo token

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,
    })
  })
})

the response is:

{"foo":{"iat":1523660987}}

@delvedor
Copy link
Member

Can you upload a full example that reproduces the issue?

@sensorario
Copy link
Author

No sorry. But I'd found the solution (or the problem) ^_^. In /signup endpoint I've sent {payload:{username:"foo"}}.

In the wrong code fastify.log.info('username : ' + decoded.username) I've fixed with fastify.log.info('username : ' + decoded.payload.username)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants