fastify-jwt #1017
-
fastify-decorators is a great plugin, and I have already applied it in our project. Now I have a question, how can I apply fastify-jwt to the controller to implement token verification for certain controllers? Thank you very much. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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)
}
}
} |
Beta Was this translation helpful? Give feedback.
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: