expressjs and protected routes with user roles
I am trying to find an example online of using express SDK and protecting routes for users with given roles.
Is there anything out there?
2 Replies
Hi Coach!
I'll admit the Express SDK documentation doesn't quite document this use case well, so I'll flag it with the team.
To start with, lets make sure you're getting roles included in your access token - to do this, you'll need to head to your Express app in the Kinde Dashboard, click Tokens and then click Customize under Access Token. Enable 'Roles (array)' and click save.
Now in your Express app, I'll assume you've got it set up similar to how the starter kit has it set up -
That
client
is the ticket we need to making this work! It'll allow us to use getClaim
to retrieve the roles
we've just added to the access token. We can also use it for getting permissions as well via client.getPermission
and client.getPermissions
if you're looking for that.
Here's some example code for you:
Please let me know if this helps you out 🙂Thanks! Will have a play with all this tomorrow.