Auth0-react user = undefined and isAuthenticated = false

Hi there, I have a problem with integrating auth0 into my React application.

Everything is done like in the official demo example, but:

  • The authentification is successful but when I am trying to get {user} or {isAuthenticated} values from the useAuth0() it returns me “undefined” and “false” as well.

Code below:

**My LoginButton.tsx file: **

import { FC } from 'react'
import { Button } from 'antd'
import { useAuth0 } from '@auth0/auth0-react'

const LoginButton: FC = () => {
    const { loginWithRedirect, user, isAuthenticated } = useAuth0()

    console.log(user) // undefined
    console.log(isAuthenticated) // false

    return (
        <Button type="primary" onClick={() => loginWithRedirect()}>
            Login
        </Button>
    )
}

export default LoginButton

** My index.tsx file **

import ReactDOM from 'react-dom/client'
import App from './App'
import { Auth0Provider } from '@auth0/auth0-react'

/** External styles */
import 'antd/dist/antd.css'
import 'normalize.css'
import React from 'react'

const providerConfig = {
    domain: '',
    clientId: '',
    redirectUri: 'http://localhost:8888/api/callback',
}

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
root.render(
    <React.StrictMode>
        <Auth0Provider {...providerConfig} cacheLocation="localstorage">
            <App />
        </Auth0Provider>
    </React.StrictMode>
)

Thanks for each reply.

@alina1 From the Auth0 React Quickstart docs, I found the following

Ensure that the SDK has completed loading before accessing the isAuthenticated property, by checking that isLoading is false .

@auth0/auth0-react has sample code to show isLoading + isAuthenticated to determine to show the Login button or Logout button

// src/App.js
import React from 'react';
import { useAuth0 } from '@auth0/auth0-react';

function App() {
  const {
    isLoading,
    isAuthenticated,
    error,
    user,
    loginWithRedirect,
    logout,
  } = useAuth0();

  if (isLoading) {
    return <div>Loading...</div>;
  }
  if (error) {
    return <div>Oops... {error.message}</div>;
  }

  if (isAuthenticated) {
    return (
      <div>
        Hello {user.name}{' '}
        <button onClick={() => logout({ returnTo: window.location.origin })}>
          Log out
        </button>
      </div>
    );
  } else {
    return <button onClick={loginWithRedirect}>Log in</button>;
  }
}

export default App;

I hope this helps resolve your issue!

Hi, thank you for your answer! Suddenly, the same happens even with this code.

Hey Alina, did you ever get this resolved? I’m having the same issue

1 Like

I’ve been testing the app for a couple of months without problem and now suddenly this has appeared. After going through the login procedure, the user is returned to my app but with isAuthenticated false. Pressing login again initiates isLoading, but there is no redirect to the Auth0 site. If the user closes the browser window (Chrome) and then reopens, the app now receives an IsAuthenticated from Auth0.

1 Like

Same here, any solution around it?

What solves to me it was to updated the lib version to the 2. There is a migration guide in their docs to help the process.

I was using the 1.8.0 and the I always getting some state errors.

1 Like

Yes,
The issue you’re describing could be caused by a few different things, but most likely it is related to the way you are using the Auth0-react library.

Here are a few things you can try:

  1. Make sure you have properly configured the Auth0Provider component in your application. This component should wrap your entire application and provide the necessary configuration options for the Auth0 SDK to work correctly. For example:

phpCopy code

<Auth0Provider
  domain="your-domain.auth0.com"
  clientId="your-client-id"
  redirectUri={window.location.origin}
>
  <App />
</Auth0Provider>
  1. Check that you are calling the useAuth0 hook correctly in your components. This hook provides access to the user and isAuthenticated objects that you are trying to access. Make sure you are calling the hook inside a functional component and that you are using the destructured values correctly. For example:

javascriptCopy code

import { useAuth0 } from "@auth0/auth0-react";

function MyComponent() {
  const { user, isAuthenticated } = useAuth0();

  if (isAuthenticated) {
    return <p>Hello {user.name}!</p>;
  } else {
    return <p>Please log in.</p>;
  }
}
  1. Verify that the user has actually authenticated with Auth0. You can check this by looking at the localStorage object in your browser’s developer tools. Auth0 stores a JWT token in local storage when a user is authenticated. If you don’t see this token, it means the user has not yet authenticated. You may need to trigger the authentication flow by calling the loginWithRedirect function provided by the useAuth0 hook.
1 Like

Just in case it’s useful, this happened to me in the past a few times and was related to the use of localhost instead of 127.0.0.1 (or custom domain) for development.

Said that, this last time, it started to happen was due to use the wrong scope in the authorizationParams for the Auth0Provider component.

1 Like