Describe the problem
I have followed code example in The Complete Guide to React User Authentication with Auth0 and noticed the behaviour of the login state not persisting on reload. After trying this on different browsers I found it only works as expected on Firefox and Chrome.
I also raised this on stack overflow without my success: Auth0 does not persist login on page refresh for email/password
What was the expected behaviour?
When a user is logged in and opens a new tab or reloads the page, the Auth0Provider should make a silent auth call to log in the user without having to make them reloading again.
Reproduction
I used the example code from the blog post with a few changes.
AuthProvider.jsx
const AuthProvider = ({ children }) => {
const history = useHistory()
const onRedirectCallback = appState => {
history.push(appState?.returnTo || window.location.pathname)
}
return (
<Auth0Provider
domain={process.env.REACT_APP_AUTH0_DOMAIN}
clientId={process.env.REACT_APP_AUTH0_CLIENT_ID}
redirectUri={window.location.origin}
onRedirectCallback={onRedirectCallback}
>
{children}
</Auth0Provider>
)
}
Routes.jsx
export default function Routes() {
const { isLoading, isAuthenticated } = useAuth0()
const getDashboard = () => {
//determine which dashboard to return
}
if (isLoading) return <p>Loading...</p>
if (isAuthenticated) {
return (
<Suspense fallback={<p>loading...</p>}>
<Switch>
<Route exact path="/">
{getDashboard()}
</Route>
<Route>
<NotFound />
</Route>
</Switch>
</Suspense>
)
}
return <SignInAndRegister />
}
SignInAndRegister.jsx
export default function SignInAndRegister() {
const { loginWithRedirect } = useAuth0()
return (
<div className="p-4 max-w-screen-md h-screen overflow-hidden flex items-center align-center mx-auto">
<div className="w-full">
<h1 className="text-xl text-center mb-6">My Website</h1>
<PrimaryButton onClick={loginWithRedirect} fullWidth={true}>
Login
</PrimaryButton>
<div className="mb-3" />
</div>
</div>
)
}
Steps to reproduce the error:
- Run app (this app is using create-react-app), so
yarn start
- Navigate to
localhost:3000
- Click login (which will navigate you to the Lock UI)
- Sign in with username/password
- App is redirected back to
/ with ?code=<code>
isAuthencated value flips to true and Dashboard component is rendered
- Reload the page
- Silent auth call fails, making the user log in again
The failed Failed Silent Auth has the information:
{
"date": "2020-08-27T07:38:48.043Z",
"type": "fsa",
"description": "Login required",
"client_id": <client_id>,
"client_name": <client_name>,
"ip": <ip>,
"user_agent": "Safari 13.1.2 / Mac OS X 10.15.6",
"details": {
"body": {},
"qs": {
"client_id": <client_id>,
"redirect_uri": "http://localhost:3000",
"scope": "openid profile email",
"response_type": "code",
"response_mode": "web_message",
"state": <state>,
"nonce": <nonce>,
"code_challenge": <code_challenge>,
"code_challenge_method": "S256",
"prompt": "none",
"auth0Client": <auth0Client>
},
"connection": null,
"error": {
"message": "Login required",
"oauthError": "login_required",
"type": "oauth-authorization"
}
},
"hostname": "<my-app>.auth0.com",
"audience": "https://<my-app>.auth0.com/userinfo",
"scope": [
"openid",
"profile",
"email"
],
"auth0_client": {
"name": "auth0-react",
"version": "1.0.0"
},
"log_id": <log_id> ,
"_id": <_id>,
"isMobile": false
}
Environment
- "@auth0/auth0-react": "^1.0.0",
- "react": "^16.13.1"
- "react-scripts": "3.4.1",
- Firefox (works), Chrome (works), Brave (fails), Safari (fails)
I have verified my app is set up with
- Allowed Callback URLs:
localhost:3000
- Allowed Logout URLs:
localhost:3000
- Allowed Web Origins:
localhost:3000
- Refresh Token Behavior: Non-Rotating
- Application Type: Single Page Application
- Token Endpoint Authentication Method: None
Describe the problem
I have followed code example in The Complete Guide to React User Authentication with Auth0 and noticed the behaviour of the login state not persisting on reload. After trying this on different browsers I found it only works as expected on Firefox and Chrome.
I also raised this on stack overflow without my success: Auth0 does not persist login on page refresh for email/password
What was the expected behaviour?
When a user is logged in and opens a new tab or reloads the page, the
Auth0Providershould make a silent auth call to log in the user without having to make them reloading again.Reproduction
I used the example code from the blog post with a few changes.
AuthProvider.jsx
Routes.jsx
SignInAndRegister.jsx
Steps to reproduce the error:
yarn startlocalhost:3000/with?code=<code>isAuthencatedvalue flips totrueand Dashboard component is renderedThe failed
Failed Silent Authhas the information:Environment
I have verified my app is set up with
localhost:3000localhost:3000localhost:3000