Auth state lost after a reload

Here is the code:

        <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:

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?

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.

1 Like