# Redirect user depending on role after login

**URL:** <https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610>\
**Category:** Get Help\
**Tags:** login, auth0\
**Created:** [May 17, 2022, 4:54pm UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610 "2022-05-17T16:54:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivo.pachecu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ivo.pachecu/32/34158_2.png) [@ivo.pachecu](https://community.auth0.com/u/ivo.pachecu)\
**Post date:** [May 17, 2022, 4:54pm UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610/1 "2022-05-17T16:54:36Z")

</div>

Hello, I am trying to understand if it s possible to redirect people after login, to a specific page checking their role. Imagine if someone is admin, will get the admin page and if someone is a user will get an user page.  
export default handleAuth({  
async login(req, res) {  
await handleLogin(req, res, {  
returnTo: “/dashboard”,  
});  
console.log(‘RES:’,res)  
},  
});

I was doing this and than in the dashboard page, i was sending a specific view, but i don t want to go to a page that is not supposed, and wanted to go directly to the right page depending on role

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [May 18, 2022, 5:38pm UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610/3 "2022-05-18T17:38:41Z")

</div>

Hi @ivo.pachecu,

Welcome to the Auth0 Community!

> [@ivo.pachecu](#):
>
> I am trying to understand if it s possible to redirect people after login, to a specific page checking their role

Yes, this is possible by using an Auth0 Post-Login Action to redirect them based on the user’s Role.

To do so, you will need to check the [`event.authorization.roles`](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/event-object) property and redirect the users using the [`api.redirect.sendUserTo()`](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/api-object) method.

See [here](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow#redirect-the-user-to-an-external-site) to learn more on how to redirect users using a Post Login Action.

Please let me know if you have any questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![ivo.pachecu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ivo.pachecu/32/34158_2.png) [@ivo.pachecu](https://community.auth0.com/u/ivo.pachecu)\
**Post date:** [May 18, 2022, 7:12pm UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610/4 "2022-05-18T19:12:49Z")

</div>

Hi, thank you for your answer, just to see if i understan, this will be called in my auth0 file?  
where i do the handle login?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [May 18, 2022, 9:27pm UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610/5 "2022-05-18T21:27:45Z")

</div>

Hi @ivo.pachecu,

Thank you for your response.

> [@ivo.pachecu](#):
>
> this will be called in my auth0 file?

This will not be called in your files and you will not need to make any changes to them. Instead you will need to configure the Action on your Auth0 Dashboard.

To do so, navigate to your [_Auth0 Dashboard \> Actions \> Flows \> Login_](https://manage.auth0.com/#/actions/flows/login/).

Please see [this doc](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow) on how to use a Post Login Action.

Hoped this helps!

Thank you.

---

<div class="post-metadata">

**Author:** ![ivo.pachecu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ivo.pachecu/32/34158_2.png) [@ivo.pachecu](https://community.auth0.com/u/ivo.pachecu)\
**Post date:** [May 18, 2022, 9:46pm UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610/6 "2022-05-18T21:46:31Z")

</div>

Oh nice, i have now implemented that, but now in my client I can t use the useUser hook, any idea why that happens?

---

<div class="post-metadata">

**Author:** ![ivo.pachecu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ivo.pachecu/32/34158_2.png) [@ivo.pachecu](https://community.auth0.com/u/ivo.pachecu)\
**Post date:** [May 20, 2022, 12:23pm UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610/7 "2022-05-20T12:23:42Z")

</div>

HEY,  
I have an action to redirect the user to the right page regarding their role, but now i try to use useUser Hook, and it s always undefined Any idea how i can fix this? If they can t be used at the same time, how can i access user information and protect routes?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [May 20, 2022, 9:27pm UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610/8 "2022-05-20T21:27:52Z")

</div>

Hi @ivo.pachecu,

Thank you for your responses.

> [@ivo.pachecu](#):
>
> i try to use useUser Hook, and it s always undefined Any idea how i can fix this? If they can t be used at the same time, how can i access user information and protect routes?

Firstly, did you confirm that you are using the `useAuth0` Hook after the user has authenticated?

Next, let me clarify that Actions and the `useUser` Hook are separate from each other. Meaning that the Action and `useAuth0` hook can be used at the same time.

Please let me know if you have any additional questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![ivo.pachecu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ivo.pachecu/32/34158_2.png) [@ivo.pachecu](https://community.auth0.com/u/ivo.pachecu)\
**Post date:** [May 21, 2022, 9:21am UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610/9 "2022-05-21T09:21:20Z")

</div>

Hey, maybe I am confused. But currently the flow that i implement is:  
I have a login button that calls “/api/auth/login” and i created an action that s like:

```auto
exports.onExecutePostLogin = async (event, api) => {

  if (event.authorization) {
    const namespace = 'https://my-app.example.com';
    api.idToken.setCustomClaim(`${namespace}/user_metadata`, event.user.user_metadata);
  

    if(event.authorization.roles[0] == 'admin'){
      api.redirect.sendUserTo("http://localhost:3000/Admin/Statistics")
    }
    if(event.authorization.roles[0] == 'user'){
      api.redirect.sendUserTo("http://localhost:3000/User/UserDashboard")
    }
       
  }
  
}

```

So after getting login, i am redirected to the right page. And in that page I am now trying to protect routes, and i don t know how, and i am trying to access user info with useUser but it always says it s undefined.

Am i missing any step?  
btw on the server side i have a spring boot api, in order to accept requests authenticated, should i only check the access token? does that confirm the user is logged in?

Many thanks, but i am kinda lost so i really need a bit of help

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [May 25, 2022, 10:57pm UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610/10 "2022-05-25T22:57:47Z")

</div>

Hi @ivo.pachecu,

Thank you for your response.

After reviewing your Action script, it looks like you need to resume authentication after the redirect to the `/continue` endpoint. If this step is skipped, Auth0 will lose the context of the login transaction, and the user will not be able to log in due to an `invalid_request` error.

See [here](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/redirect-with-actions#resume-the-authentication-flow) to learn more about resuming the authentication flow after a redirect.

Please let me know if you have any questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [October 26, 2022, 3:23pm UTC](https://community.auth0.com/t/redirect-user-depending-on-role-after-login/83610/11 "2022-10-26T15:23:07Z")

</div>


