# Auth state lost after a reload

**URL:** <https://community.auth0.com/t/auth-state-lost-after-a-reload/96867>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [December 21, 2022, 1:57am UTC](https://community.auth0.com/t/auth-state-lost-after-a-reload/96867 "2022-12-21T01:57:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tison](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/tison/32/42103_2.png) [@tison](https://community.auth0.com/u/tison)\
**Post date:** [December 21, 2022, 1:57am UTC](https://community.auth0.com/t/auth-state-lost-after-a-reload/96867/1 "2022-12-21T01:57:58Z")

</div>

Here is the code:

```plaintext
        <Auth0Provider
            clientId={`${NEXT_PUBLIC_AUTH0_CLIENT_ID}`}
            domain={`${NEXT_PUBLIC_AUTH0_DOMAIN}`}
            redirectUri={resolveRedirectUri()}
            onRedirectCallback={(appState?: AppState): void => {
                if (appState) {
                    history.push(appState.location);
                }
                window.history.replaceState(
                    {},
                    document.title,
                    appState?.returnTo || window.location.pathname
                );
            }}
        >
            <Authenticator children={children}/>
        </Auth0Provider>

```

and `Authenticator`:

```auto
export default function Authenticator({children}): JSX.Element {
    const {isAuthenticated, error, user, loginWithRedirect} = useAuth0();

    if (error) {
        return <Errors code={500} message={error.message}/>;
    }

    if (!isAuthenticated) {
        const location = useLocation();
        return <>
            <Head>
                <meta property="og:description" content="Login page"/>
                <meta charSet="utf-8"/>
                <title>登录</title>
            </Head>
            <div className={styles.container}>
                <main className={styles.main}>
                    <h1 className={styles.title}>
                        《社群长青》
                    </h1>

                    <p className={styles.description}>
                    <Button variant={'contained'} onClick={() => loginWithRedirect({
                        appState: {
                            location: location,
                            returnTo: location.pathname,
                        }
                    })}>
                        点击登录
                    </Button>
                    </p>
                </main>
            </div>
        </>
    }

    if (isWhitelistUsers(user)) {
        return <>{children}</>;
    }

    return <Errors code={403} message={`Unregistered user: ${JSON.stringify(user)}`}/>;
}

```

When I successfully authenticate, after a page reload (ctrl + R) the login state is lost, and I have to re-auth.

How can I keep the login state within the same window?

---

<div class="post-metadata">

**Author:** ![Ferneside](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ferneside/32/33806_2.png) [@Ferneside](https://community.auth0.com/u/Ferneside)\
**Post date:** [December 21, 2022, 5:51am UTC](https://community.auth0.com/t/auth-state-lost-after-a-reload/96867/2 "2022-12-21T05:51:41Z")

</div>

I just want to say why the code shown doesn’t seem related to your issue. Can you show us where the localstorage is set and used[.](https://www.tellculvers.onl/)

---

<div class="post-metadata">

**Author:** ![remus.ivan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/remus.ivan/32/68771_2.png) [@remus.ivan](https://community.auth0.com/u/remus.ivan)\
**Post date:** [January 31, 2025, 6:50pm UTC](https://community.auth0.com/t/auth-state-lost-after-a-reload/96867/4 "2025-01-31T18:50:15Z")

</div>

Hi @tison,

Welcome to the Auth0 Community and sorry for the late reply.

There could be multiple reasons as of why this issue occurs given the current implementation, but in order for the user to remain logged in after refreshing the page the _query parameters_ in `state` or `code` must be removed from the URL. This can be accomplished by exchanging the `appState?.returnTo || window.location.pathname` to just `'/'` in your code, as mentioned in our current [documentation](https://auth0.com/docs/quickstart/spa/vanillajs/01-login#evaluate-the-authentication-state).

Otherwise you can also explore other options such as :

1. **Silent authentication** can be used in order to generate new tokens as long as the user still has a valid session. This can be accomplished by making a call to the `/authorize` endpoint with the parameter `prompt=none`, with more details that can be found under [this documentation](https://auth0.com/docs/authenticate/login/configure-silent-authentication#renew-expired-tokens). However Silent authentication requires third-party cookies supplied by Auth0, which are generally blocked by specific browsers such as Safari. An alternative would be useing Auth0’s [Custom Domain](https://auth0.com/docs/custom-domains) functionality to prevent browsers from blocking Auth0’s cookies.

2. A workaround for this is to use [Refresh Token Rotation](https://auth0.com/docs/tokens/refresh-tokens/refresh-token-rotation) and set the `cacheLocation` to **localstorage** when initializing the Auth0 client. However when using “localstorage” it is recommended that the Access Token’s life is shortened as much as possible. An example would look like this :

```auto
auth0 = await createAuth0Client({
domain: config.domain,
client_id: config.clientId,
useRefreshTokens: true,
cacheLocation: 'localstorage'
});

```

Thanks for posting your inquiry and I hope this helped.  
Best regards,  
Remus

---

<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:** [February 14, 2025, 6:51pm UTC](https://community.auth0.com/t/auth-state-lost-after-a-reload/96867/5 "2025-02-14T18:51:04Z")

</div>

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