How to redirect admins to a specific page?

Is there a rule I can use to redirect all users with the role admin to example.com/admin ?

Ofc this url should only be accesible by admins!

Hi @gutisAlex,

Thanks for joining the Community!

I will see if I can get a rule working for that. You may have already found this, but just in case, here is some documentation after log-in redirects: Redirect Users

As suggested in Once user authenticated, how do I redirect the url based on role? - #2 by dan.woda, one solution would be to pass the role as a custom claim in the ID Token and redirect the user based on their role.

1 Like

The guy himself said it can be done in a numerous ways… I am already passing the roles via token and I probably would have some ideas to implement a way into my app to redirect an admin to the admin page but how about Redirect Users from Within Rules ? I managed to redirect an Admin to the page by simply using this rule:

function (user, context, callback) {
  if(context.authorization.roles.includes('Admin')) {
  	context.redirect = {
  		url: "http://localhost:8080/admin"
  	};
  }
  return callback(null, user, context);
}

Only problem right now is I am running into and endless loop… I didnt quite understand the part where I have to redirect to the continue endpoint… So when going on localhost:8080/admin I have to take the state from the url and redirect to localhost:8080/continue?state=THE_ORIGINAL_STATE and the auth0 side now would know all is fine and redirect me back to my admin site eventhough I dont even have that endpoint?!

So basically before anything gets rendered on my admin page I check if there is a state in the url, if so I redirect to localhost:8080/continue?state=abc123 and if not I stay?!

I probably have to extend the rule by the validate resumed login rule also?

The Redirect Users from Within Rules functionality takes place before authentication is finished. It is primarily used as a way to additional steps to the user registration process such as a privacy agreement step. The user is not logged until you redirect them to the https:YOUR_AUTH0_DOMAIN.com/continue?state=THE_ORIGINAL_STATE.

It would be your Auth0 tenant’s /continue endpoint instead of your app’s.

Only one redirect can take place during the login process via a rule.

The redirect rule doesn’t seem like the functionality you are looking for since you’d like to redirect the user after authentication. Whereas the redirect rule is for adding additional steps to the login process.

Ok I worked out a solution on my end using react hooks…

So the Redirect Users From Within Rules functionality would come in handy if I have smth like an onboarding, where a user has to provide additional infos, like an address, nationality, ect.?! So when visiting my site I am getting redirected to onboarding, put in extra information, which I send to my own backend, and if that worked I redirect to myapp.eu.auth0.com/continue?state=123 and the authentication would continue sending me to the main page?! Is that the flow of this feature? I probably need a custom prop on auth side to check if hasOnboarded or not?!

Yes, that is correct! The redirect users from within rules functionality would be helpful for an onboarding step. Yes, you are right about including the hasOnboarded property. You could store this in the user’s app_metadata and check for the key before changing the redirect URL.

ok but how am I setting up the onBoarding prop to every new user registration? I was reading the https://auth0.com/docs/users/set-metadata-properties-on-creation article but I dont really understand it… I am using the universal login, and over this people register as well, I dont really have anything to do with the whole registration process… So where am I setting this up?

Edit: I managed to set the prop via hooks…

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