onRedirectCallback based on info from user object


I’m trying to handle a redirect based on information retrieved from the user object in my React App. So I have created a rule that defines if a users is a first-time login or not. Based on the information I want to redirect users to different pages.

My problem is that the user object hasn’t been defined once the onRedirectCallback is running, hence I can’t determine if it’s a first-time user or not.

Is there any other way I can approach this?

In the rule, you may insert a custom claim in the ID Token if the user is logging in for the first time. On the redirect callback handler of your app, you may check the ID Token for this claim, and based on this, the app may redirect the user to a different page.

You may find a sample for how to insert a custom claim on this link.

1 Like

I don’t get this solution since the redirect callback handler is set on the login event hence before the user is authenticated? So I can’t check the ID Token on the callback since I’m not logged in?

As of I understand I could be able to handle the redirect on useEffect or similar. However, since the loginWithRedirect is redirecting me to either what is set in the onRedirectCallback or redirectUri I’m not able to set this.

I forgot to mentioned earlier that I’m using @auth0/auth0-react

Hi, @linus1 on your react application login handler URL will be the same both for the first time login as well as the consecutive logins. However, once the authentication is completed, on the callback handler you may decode the ID Token and find the custom claim inserted in the rule.

If the claim is available, meaning that it is the first time login for the user, the app may initiate a second redirect to the endpoint where you handle the first time login use case.

Let me know if you have any questions.


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