5

Auth0 Pipeline Rule to add Roles to a User - Knoldus Blogs

 2 years ago
source link: https://blog.knoldus.com/set-roles-to-user-in-auth0-using-rules/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to add a role to a user in Auth0 dynamically using Auth pipelines Rules :-

Reading Time: 4 minutes

Tired of adding Roles to every new User, who wants to access your particular service, manually by logging into auth0. And giving it permission to access the service by attaching the Roles to it. And are you looking for a better approach to it. Like a Role automatically getting attached to all the valid users of a particular organisation as soon as they login. Then you are at the right place. In this blog I will be explaining you how you can achieve the same using Auth pipelines Rules.

Make sure to have a User and a Role created :

It is very simple to create a User and a Role in Auth0. And for your reference I have attached the link to my blog in which I have explained it in detail, on how to create a User and a Role in Auth0. After you have created a user you can scroll through a bunch of sections available. Navigate to the Details section, to the Permissions section and to the Roles section.

B1VU52SuRZ7GXTy-77D1QGKUt0c5Mj5E534ydkmxUMiYLGQAOz7mGsZi4B8UPxNbOqSs-vSjwHfIt9arTJOEOkMCrXkyGW93iXb2uoiDYP3PGjjg4SEc_zUVQ-x5lC5eM0eai4U5

Ans as you can see currently no Roles are assigned to the user, which will automatically be assigned after the Rule that we will write gets executed.

6WVQ_MjTLHMDpHg6wDVu7Nz869e2jpcBXA3iVXEU0uSj3p4TfT1Zuq9Q7YSNX4TC26G59bT7p_pS1LSGID9BP2GW0J7M5fbKPoBYBa2aJFP3e3TkCT8gCW-NHDjZemuE3Lyj6X6v

Similarly, we have a Role created with the name Test-Role. This role has currently only one Permission and is using API named Demo-API which is our custom API that we have created for this demonstration purpose only. Currently no user is assigned to this Role.

Now lets move to the main topic of the blog i.e. Rules.

Creating an Auth Pipeline Rule :

Before creating our first Rule, lets first discuss what actually a Rule is. Rules are nothing but code snippets written in javascript that run on auth0 servers. We can customised them based on our requirements.

Consider it as a function that passes three parameters. The first parameter is user object as it come from the identity provider, the second parameter is the context object that contains the information about the current login attempt (from which device the log in made, which application a user is trying to login), and the last is the callback function that must be called within the rule to indicate either success or error. Auth0 provides simple way to customise the login process with the feature we call Rules.

-xaIr5qRR2P2n_6CpuOBk5IkrTcIr0Ccqb3xH0SgX2PIahJ1fn7aonSquffBRq5wIjYhLQlAeOm7SNhchxUA2wVsqY957AT47d9x9mSA7ZAdusCgYMiO9k8UTku_YBCNo0LF6DE0

To create a new Rule, Click the create Rule button. Which will take to the page where we will find options that we can use and customised according to our need in order to create a Rule. We can either pick an empty rule allowing us to build a rule from scratch or can choose from existing templates.

Lets pick empty rule for our demonstration as we will be writing our customised rule.

v_MzSCl3zGSkWCuSrMU8_Juv2jHZqeE3NQnt3e5FnbA3QcLCZYiWupeqxEj_fLJXreMPMwtLCHbRtYzhYEixcpihFuJJXkIEhfonj6zSvu0uuxf2lmuQO22zTOdJooeKrvBXajqX

m1lo-eryp8s5AP5FiEsGE3wbAuGfXwKoXKhJLKsYR9CmXt9ztqWppJYFmuVipjAMrGe0Yu3rO0mlOXvqi8aw_mG53IfmeE6E96rCOPkWTzBHkZOx1x1ZmlkIdtX3PTNjKR2aB8Sz

This template has an if condition attached to it which checks if the user is a valid user by checking its email endpoint, then has a function assigned to variable assignRoletoUser which is taking the logged-in user User-id and a role-id of the Role we want to attach and is assigning that Role to that particular User. We can give here a list of Roles in array format, all will get attached in the same way. Also at the end, a try-catch function for catching any errors in case it occurs.

Role getting automatically attached :

deinQm7aXU71V9pQ2ZqDOJQCm0uI698eoXhCsjCMJxPLaYtZ1h8sHjarasuUQvrR4OXfQuerqMlG_-laN3EvVhHItgOMkAE4yc7QsFg2_KxpQ8lt91oBGQtfuWBeYSShpBQjfKQz

Now, when I tried login to the service this time, I can see I have access to the permissions that are mentioned in my Role. And looking at the Auth0 User we can find Role getting attached to this particular user dynamically without me doing any thing. Even if i delete this Role it will get attached again when i will log in again.

In this way you have ease your effort to attaching Role to every new User from your organisation.

References :

Link : https://auth0.com/blog/introducing-auth0-hooks/

Link to my blog on how to create an API, a User and a Role: https://blog.knoldus.com/creating-auth0-api-user-role/

knoldus footer


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK