Skip to content

fastify-jwt #1017

Answered by L2jLiga
yuanye2005king asked this question in Q&A
Discussion options

You must be logged in to vote

Hi there, according to fastify-jwt docs you can verify token inside onRequest hook. On fastify-decorators side exists [Hook] decorator which can be used to define hook in controller. Each controller registers in fastify as plugin and hence each controller has own scope, so hooks defined in controller will work only in controller where you define them.

Snippet:

@Controller('/protected')
class ProtectedController {
  @Hook('onRequest')
  async (request, reply) => {
    try {
      await request.jwtVerify()
    } catch (err) {
      reply.send(err)
    }
  }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by L2jLiga
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1016 on July 22, 2024 16:16.