feat: add validateDecoded option to validate JWT payload#378
feat: add validateDecoded option to validate JWT payload#378williamfds wants to merge 2 commits intofastify:mainfrom
validateDecoded option to validate JWT payload#378Conversation
|
Hello again @jsumners Following up on your feedback in #377, I’ve created this new PR after renaming the branch; This new PR:
Thanks again for your previous review! Would love your input here to move this forward 🙏 |
|
Please don't open new PRs for the same work. The context of the conversation around the work gets lost. Update the original PR. |
Hi! This won't happen again. Thanks for your understanding! |
|
The source branch name is really inconsequential. Please keep the work in the original PR. |
|
Thank you. |
This PR introduces a new
validateDecodedoption to the@fastify/jwtplugin.It allows developers to define custom validation logic that runs after the JWT is decoded and verified, but before assigning
request.user.This enables scenarios where claims alone are insufficient, such as:
Checking flags (e.g.,
isVerified)Validating roles or permissions
Applying business rules or JSON Schema validation
Performing async checks (e.g., database lookups)
Example: synchronous usage
Example: asynchronous usage
Implementation notes
This change includes the following:
Adds
validateDecoded(payload)support to plugin optionsExecuted after token verification in
request.jwtVerify()If validation fails, responds with 400 Bad Request
Includes test coverage for both sync and async cases
Type definitions updated (
types/jwt.d.ts)Documentation updated in
README.mdRelated
Checklist
npm run testandnpm run benchmarkand the Code of conduct