feat: add validateDecoded option for JWT payload validation#377
Open
williamfds wants to merge 2 commits intofastify:mainfrom
Open
feat: add validateDecoded option for JWT payload validation#377williamfds wants to merge 2 commits intofastify:mainfrom
validateDecoded option for JWT payload validation#377williamfds wants to merge 2 commits intofastify:mainfrom
Conversation
jsumners
requested changes
May 29, 2025
Member
jsumners
left a comment
There was a problem hiding this comment.
I'm not sure this is a good idea. The issuance of a JWT should be done after the client has been authenticated and authorized. This seems to be adding authorization functionality into the JWT validation process. I think that is probably a bad practice.
If this change is warranted, I think the naming of the option is not clear enough. And this is definitely missing required documentation.
4 tasks
validateDecoded option to validate JWT payload
validateDecoded option to validate JWT payloadvalidateDecoded option for JWT payload validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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