Skip to content

Failed Silent Auth on page reload #101

@charklewis

Description

@charklewis

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:

  1. Run app (this app is using create-react-app), so yarn start
  2. Navigate to localhost:3000
  3. Click login (which will navigate you to the Lock UI)
  4. Sign in with username/password
  5. App is redirected back to / with ?code=<code>
  6. isAuthencated value flips to true and Dashboard component is rendered
  7. Reload the page
  8. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions