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.
Most of the time, when I work with Keycloak, I need to protect some routes or even some parts of my views based on the roles associated to the user on keycloak.
But, unfortunately, we don't have any method or even function to retrieve directly user roles in this package, unless we make some request to keycloak.
I thought that it can be hepful for the community to have such method so I write it. It just decode the access_token to retrieve the user roles.
To decode the token I use the
firebase/php-jwt
package. But if you don't want more dependency, I can do it by my own.To decode the token, I use also the realm's public key which the user can provide via the env variable
KEYCLOAK_PUBLIC_KEY
. But it's optional actually. So, when it's not set, I retrieve it by my own.I hope that you will appreciate it and please let me know if you find any issue or suggestion on my code.
Regards