Redirect user depending on role after login

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

Hi @ivo.pachecu,

Welcome to the Auth0 Community!

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 property and redirect the users using the api.redirect.sendUserTo() method.

See here to learn more on how to redirect users using a Post Login Action.

Please let me know if you have any questions.

Thank you.

2 Likes

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?

Hi @ivo.pachecu,

Thank you for your response.

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.

Please see this doc on how to use a Post Login Action.

Hoped this helps!

Thank you.

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

1 Like

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?

1 Like

Hi @ivo.pachecu,

Thank you for your responses.

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.

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:

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

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 to learn more about resuming the authentication flow after a redirect.

Please let me know if you have any questions.

Thank you.