-
Notifications
You must be signed in to change notification settings - Fork 261
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
feat: add ability to check JWT claims #211
Conversation
Requesting notes from: @adamjmcgrath (sorry to ping) [@Vivalldi: So sorry I tagged you, thought you were a code owner 😅] |
Hi @ctjlewis - thanks for raising this PR, checking claims to access routes would be a great addition to the SDK. I have a request about the implementation - could you change it to accepting function that returns a boolean to indicate if the claim check passes, eg
const WrappedComponent = withAuthenticationRequired(MyComponent, {
claimCheck(claims) {
return claims['https://my.app.io/jwt/claims'].includes('ADMIN')
}
}); (The name |
It's a pain to link packages that use react hooks - you could try pointing Or just try using |
That made the code change much smaller and more straightforward, probably the best approach. Please lmk what you think about the changes. |
Remove string -> string[] coercion, renamed to `claimCheck`, update tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good - couple of suggestions
Though this assumes JWT claims will be |
Hi @ctjlewis If you change the signature of claimCheck to accept a User I'd be happy to proceed with this PR |
Please let me know if these changes are sufficient. |
Co-authored-by: Adam Mcgrath <[email protected]>
Thank you very much for your help on this Adam. I merged your suggestions. |
The changes in
|
Am not seeing that error on the CI server - perhaps you're using a different TS compiler than the project. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm - thanks @ctjlewis!
Description
I wanted to easily protect a route not only for logged-in users, but also for those with a given
ROLE
JWT claim. I figured I'd send this PR in for review in case this team was interested in merging the changes, or some version of them.Usage
Example of an unauthorized route request. User with
['USER', 'MODERATOR']
claim trying to access a protected component that is only forROLE
claims that include'ADMIN'
:auth0-react/__tests__/with-authentication-required.test.tsx
Lines 44 to 60 in e13556b
Example of an authorized route request, demonstrates claim
string -> string[]
coercion, such thatROLE: "ADMIN"
matchesROLE: ["ADMIN"]
etc.auth0-react/__tests__/with-authentication-required.test.tsx
Lines 108 to 121 in e13556b
Testing
Unit tests were added to ensure that protected routes only showed to users with all specified JWT claims. If
requiredClaims
inWithAuthenticationRequiredOptions
is falsy, the claims check automatically passes.Checklist
master