Get current user role
Hello!
Im using NextJS 15 with Kinde auth (love it so far)
I have been looking through older posts about how to get the current users role, but I have not yet succeeded to find a solution.
My goal is to get the role when fetching "getUser" through "getKindeServerSession".
I have made sure to create desired roles and assigned them.
Maybe i have not really understood the concept of how to use roles properly in Kinde Auth, im currently studying front-end dev so sorry if the answer is rather simple π
Thanks in advance!
EDIT / ADDITION:
While reading the documentation i see that Kinde recommend controlling access to features on permission-level -> We recommend you use permissions for controlling access to features within your application, then use roles to group the permissions.
But for interest i still would like to know if above is possible.
4 Replies
Hello to you!
You can absolutely access roles - you'll just need to make a configuration change.
In your Kinde Application (on the Dashboard) click Tokens on the left, scroll down to Token customization and click customize on Access Token. Enable "Roles (array)" and click save.
Now to access the roles in your code, you can use
getAccessToken
returned from getKindeServerSession
. This will include the users roles in an array that looks like so:
I've attached some images to help direct you. Hope this helps!Thank you so much, I never considered to use the access token like this , focusing so hard on the USER object .
Thank you again , I will try this ASAP π€
Glad to hear it! Let me know how you get on π
Works like a charm, thank you very much π