How can I redirect the user based on his role after logging in?

After a user logs in, I must redirect him to a specific route based on his user role.

Should this be implemented in the Auth0 Actions or our React Application?

I’ve tried setting properties to the idToken and the accessToken in the auth0 post login actions, but I’ve had no luck retrieving them on the front end so far.

image

Is this the way to go, or should I do this in our react application?

If this is the way to go, why am i not able to attach the user-role property to the tokens?

I’ve also tried to use the api.redirect function, but no luck there as well. I couldn’t find concrete examples regarding my use case.

Any help is welcome, thanks!

Hey there @borjan.kalinoski welcome to the community!

This is a valid approach and outlined in this topic - It’s hard to tell from the code snippet shared. Are you defining the roles variable elsewhere?

hi @tyf

For now, the roles variable has a hardcoded value like: const roles = 'test'; // or ['test']; Ideally, I would fetch the user role somehow from auth0, but I can get back to that later.

From what I understood, i need to do:

  1. Call api.redirect.sendUserTo() in the onExecutePostLogin, in my case sendUserTo('localhost:3000/organizations
  2. Then, in my application when the /organizations route is called, I need to navigate the user to https://my-auth-domain.com
  3. Then i need to do something in the onContinuePostLogin script, which im not sure why.

I am not quite clear if these are the correct steps, and if they are, im not exactly sure what i need to do in the onContinuePostLogin script, and what kind of data do i need to pass to that script, and to the /continue route.

I can revisit this tomorrow and get back with more details if needed.

Any help is welcome!
Thanks!

I’ve managed to make it work by adding the user_roles property to the idToken.

Here are screenshots for anyone who is facing the same issue:

1 Like

Great! Thanks for sharing with the community :cowboy_hat_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.