Navigate the user directly to the login page if not authenticated

Wondering how to go to the login page without pressing a button. In the examples, the button is always provided and you have to trigger loginWithRedirect() by clicking a button.

This issue is still valid.

The code below leads to an infinite loop.

if (!isAuthenticated) {
    loginWithRedirect();
}

My code:

import React from 'react'
import { App } from './views/App'
import { useAuth0 } from '@auth0/auth0-react'

export const App = () => {
  const { loginWithRedirect, isAuthenticated } = useAuth0()

  if (!isAuthenticated) {
    loginWithRedirect()
  }

  return <App />
}

Hi @Maikki,

Did you ever resolve this issue? I am having the same problem and curious what you did to get around this.

Hi @Maikki / @notronwest,

Did either of you find a solution to this?

I’d like to redirect users to the login page if they hit my /login callbackUrl directly, but it also leads to an infinite loop.

I’ve tried using conditions with the isAuthenticated, user and isLoading props, but isLoading is not set to false when a user is not authenticated.

Using only isAuthenticated and user leads to an infinite loop and ignores my code to handle actual the actual login process because isLoading and isAuthenticated are true and false, respectively, for a few seconds.

It seems it’s been discussed recently amongst the Auth0 engineers.

Alex

If you are using the React-SDK you should be able to solve the issue by using the withAuthenticationRequired-HOC