# App infinitely redirecting after login

**URL:** <https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798>\
**Category:** Get Help\
**Created:** [May 14, 2021, 5:46am UTC](https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798 "2021-05-14T05:46:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![truescope](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/truescope/32/12814_2.png) [@truescope](https://community.auth0.com/u/truescope)\
**Post date:** [May 14, 2021, 5:46am UTC](https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798/1 "2021-05-14T05:46:46Z")

</div>

I have a user that is being infinitely redirected in our app. The webpage loads auth0, shows a pop up, then redirects the user again. It doesn’t end.  
Every time it redirects, the ‘state’ value is different. Is there some way to decode this “state” and see what its doing?  
Here’s a sample of ONE of address/state that we managed to grab during the infinite redirect:

```auto
https://[my website address]/?code=jPs0l9kQRmacT-wz&state=NGdZNnFSV25sMnMyOElXd1JJZHZ0MmZLdkRKb0Q5ckNEZGVIUDdYeFpCbw%3D%3D

```

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 14, 2021, 5:01pm UTC](https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798/3 "2021-05-14T17:01:02Z")

</div>

Hi @truescope,

The state is only encoded if something is set by the client application in the `/authorize` request.

For example, when the client application passed the state `abc123` in the authorization request:

```auto
tenant.auth0.com/authorize?...&state=abc123

```

Then it will be passed back to the client application with the redirect to the callback URL:

```auto
/callback?...&state=xyzABC123

```

You can read more about the state param in our docs:

> **[Prevent Attacks and Redirect Users with OAuth 2.0 State Parameters](https://auth0.com/docs/secure/attack-protection/state-parameters)**
>
> Describes how to use the state parameter in authentication requests to help prevent CSRF attacks and restore state

In order to help troubleshoot what might be causing the loop, would you mind sharing which SDK you are using in your app?

Have you been able to replicate the issue in an incognito window? This behavior could be caused by blocked third-party cookies.

Are you seeing any related tenant logs of failed silent auth?

Thank you,

Stephanie

---

<div class="post-metadata">

**Author:** ![truescope](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/truescope/32/12814_2.png) [@truescope](https://community.auth0.com/u/truescope)\
**Post date:** [May 16, 2021, 11:52pm UTC](https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798/4 "2021-05-16T23:52:35Z")

</div>

Hi @stephanie.chamblee  
Thanks for your response.

I am using the “@auth0/auth0-react” library, v1.4.0.  
I can easily reproduce the issue by

- open a new incognito window using chrome v90.0.4430.212
- navigate to my website https://[my site]
- auth0’s sign in form appears in the page, so I enter the details and select ‘login’

Then:

- it logs in
- the site is redirected to `https://[my site].com/?code=[my code]`
- but i think auth0 then tries to redirect the browser back to auth0’s login page, however, we’re already logged in, so it redirects back to the app. In the brief second that the window is redirecting, you can see “pop up blocked” in the address bar.

It seems to be fine for

- chrome in normal browsing mode
- firefox in normal browsing mode
- firefox in ‘private browser’ mode

Maybe chrome in cognito isn’t passing the logged in state back to the react app properly? Any tips on how we can address this?

**update 1** : it looks like this thread has the exact same issue as us:

> [@Unable to login to AUth0 in incognito mode Keeps reloading the browser window](https://community.auth0.com/t/unable-to-login-to-auth0-in-incognito-mode-keeps-reloading-the-browser-window/48475):
>
> The issue happens with any kind of login being used in Auth0 whether Google or Linkedin to sign in it works fine in a normal Browser but not in incognito Video showing how it behaves in incognito is as below This works fine in a normal browser Pl use the link to try it by using LinkedIn login [https://linkedin-app-dev.azurewebsites.net/](https://linkedin-app-dev.azurewebsites.net/) Regards Vinita

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 17, 2021, 12:30pm UTC](https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798/5 "2021-05-17T12:30:42Z")

</div>

Thank you for the additional context, @truescope!

In the linked topic and in this case, it sounds like blocked third-party cookies could be the cause. To confirm, you can try adding the `cacheLocation` config to the `Auth0Provider` if you haven’t done so already:

```auto
const onRedirectCallback = (appState) => {
  history.replace(
    appState && appState.returnTo
      ? appState.returnTo
      : window.location.pathname
  );
};

ReactDOM.render(
  <Auth0Provider
    domain={config.domain}
    clientId={config.clientId}
    redirectUri={window.location.origin}
    audience={config.audience}
    scope="read:current_user update:current_user_metadata"
    onRedirectCallback={onRedirectCallback}
    useRefreshTokens={true}
    cacheLocation="localstorage" // <-- add this config
  >
    <App />
  </Auth0Provider>,
  document.getElementById("root")
);

```

Also, are there are any places that you are calling `getAccessTokenSilently`? If so, any scope or audience passed should be listed in the `Auth0Provider`.

Note: there are security considerations for using local storage to store tokens: [Token Storage](https://auth0.com/docs/security/data-security/token-storage#single-page-app-scenarios)  
If this is the issue, using a [Custom Domain](https://auth0.com/docs/custom-domains) is the recommended approach.

[https://community.auth0.com/t/why-is-authentication-lost-after-refreshing-my-single-page-application/56276](https://community.auth0.com/t/why-is-authentication-lost-after-refreshing-my-single-page-application/56276)

---

<div class="post-metadata">

**Author:** ![truescope](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/truescope/32/12814_2.png) [@truescope](https://community.auth0.com/u/truescope)\
**Post date:** [May 18, 2021, 11:19pm UTC](https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798/6 "2021-05-18T23:19:44Z")

</div>

I’ve followed your recommendation by

- adjusting the `<Auth0Provider .../>` to include `useRefreshTokens={true}` and `cacheLocation="localstorage"`
- ensured that any scopes are also passed into the Auth0Provider (yes we do use `getAccessTokenSilently`)
- we do actually use a custom domain

But it still seems to be stuck in an infinite redirect loop.

I’ve actually managed to isolate the issue into a new react app. (made with create-react-app)

index.js:

```auto
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import './index.css';
import { Auth0Provider } from "@auth0/auth0-react";
import config from './config.json';

const onRedirectCallback = (appState) => {
	window.history.replaceState({}, document.title, appState && appState.targetUrl ? appState.targetUrl : window.location.pathname);
};

ReactDOM.render(
  <Auth0Provider
  	connection={config.connection}
    domain={config.domain}
    clientId={config.clientId}
	auddience={config.audience}
    redirectUri={window.location.origin}
	onRedirectCallback={onRedirectCallback}
	useRefreshTokens={true}
	cacheLocation="localstorage"
  >
    <App />
  </Auth0Provider>,
  document.getElementById("root")
);

```

App.js

```auto
import logo from "./logo.svg";
import "./App.css";
import { useAuth0 } from "@auth0/auth0-react";
import { useEffect } from "react";
import config from './config.json';

const App = () => {

	const { isAuthenticated, isLoading, loginWithRedirect, logout, getAccessTokenSilently } = useAuth0();

	useEffect(() => {

		if(isLoading){
			console.log('app is loading');
			return;
		}

		if(!isAuthenticated){
			console.log('not authenticated. redirecting to login');
			loginWithRedirect();
			return;
		}

		//this causes an infinite loop:
		//getToken(config.customApi);

		//this causes an infinite loop:
		//getToken(config.managementApi);

		//this works fine
		getToken({});

	}, [isLoading, isAuthenticated]);

	const getToken = (options) => {
		console.log(`retrieving token for '${options.audience}'`);
		getAccessTokenSilently(options).then(token => {
			console.log('retrieved token!', token);
		})
		.catch(e => {
			console.error('failed to get token!', e);
		});
	};

	if(isLoading){
		return (<div>loading</div>);
	}

	if(!isAuthenticated){
		return (<div>you're not authenticated!</div>);
	}

	const handleLogoutClick = (e) => {
		e.preventDefault();
		logout();
	}

	const render = () => {
		return (
			<div className="App">
				<header className="App-header">
					<img src={logo} className="App-logo" alt="logo" />
					<p>
						Edit <code>src/App.js</code> and save to reload.
					</p>
					<button onClick={handleLogoutClick}>Log out</button>
				</header>
			</div>
		);
	};

	return render();
};

export default App;

```

config.json:

```auto
{
	"connection": "app",
    "domain": "ts-app-dev.au.auth0.com",
    "clientId": "(my client id)",
	"audience": "https://domain.com/api/v2/",
	"customApi": {
		"audience": "https://api.identifier"
	},
	"managementApi": {
		"audience": "https://domain/api/v2/"
	}
}

```

And the only package.json deps you need are:

```auto
    "@auth0/auth0-react": "^1.5.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3"
  },

```

Above - if you uncomment the lines App.js, you’ll get an infinite loop.  
It seems that if you provide the `audience` option to `getTokenSilently({ audience: ... )`, while in “incognito mode” you get stuck in an infinite loop.

Note: in auth0 management interface, i have

- a react app
- the auth0 management api (i dont use this except for allowing users to reset their password)
- a custom api (which contains all of my app’s functionality)  
So i have 2 audiences (one for custom api, one for management api)

Any suggestions?  
I can send you guys the fully zipped project + a log in for my app if you like.

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 19, 2021, 3:59pm UTC](https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798/7 "2021-05-19T15:59:01Z")

</div>

Hi @truescope,

Thank you for sending this info and the code! I was able to spin up the example app.

In the `Auth0Provider`, if you update `auddience` to `audience`, you’ll be able to use:

```auto
getToken(config.managementApi);

```

Without an infinite loop.

When your app requests a token that has a different audience than what is specified in the `Auth0Provider`, the SDK will attempt to perform silent authentication to obtain an Access Token with the correct audience. However, if the app domain and your Auth0 tenant domain differ, than the browser may block the transfer of third-party cookies.

Since you are using a custom domain, this should not occur. I will have to investigate your settings some more to see what might be going on for sure.

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [May 19, 2021, 5:15pm UTC](https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798/8 "2021-05-19T17:15:31Z")

</div>

Hi @truescope,

Are you passing the custom domain as the domain in the `Auth0Provider`?

> **[Configure Features to Use Custom Domains](https://auth0.com/docs/customize/custom-domains/configure-features-to-use-custom-domains)**
>
> Learn how to configure a custom domain for Auth0 authentication features.

---

<div class="post-metadata">

**Author:** ![truescope](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/truescope/32/12814_2.png) [@truescope](https://community.auth0.com/u/truescope)\
**Post date:** [May 20, 2021, 5:25am UTC](https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798/9 "2021-05-20T05:25:17Z")

</div>

Ahhh! Spelling error in `auddience`!

After fixing that, I was able to confirm that the overall issue was incorrectly using the management api’s audience, instead of my custom api’s audience as the `Auth0Provider` audience value. It seems to only appear as an issue in Chrome’s incognito mode.

Thanks for your help @stephanie.chamblee

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [June 4, 2021, 5:25am UTC](https://community.auth0.com/t/app-infinitely-redirecting-after-login/62798/10 "2021-06-04T05:25:34Z")

</div>

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