Go to login page without pressing a button

Hi everyone,

I am wondering how to go to login page with pressing a button.
In the example code that is always provided, to go to the login page, I need to create a button and trigger it from there.

import React from “react”;
import { useAuth0 } from “@auth0/auth0-react”;

const LoginButton = () => {
const { loginWithRedirect } = useAuth0();

return <button onClick={() => loginWithRedirect()}>Log In;
};

export default LoginButton;

Instead of doing it on button press, is it possible to go to login page when the website is loaded first time and no one else is currently login?

Thank you in advance.

Hi @msuguitan,

I tried this and it seems to work without requiring a button press:

if (!isAuthenticated) {
    loginWithRedirect();
}

Depending on how you are setting up your app, you may want to return a loading component or something so your page doesn’t render for a moment.

1 Like

Hi @dan.woda,

I tried this and one enclosed in a useEffect pattern, but after successfully logging in the entire app will go into a loop. Please see screenshot below.

The authorize?audience keeps on replying cancelled over and over.
The app is not showing.
Is there a reason for the authorize?audience to loop and be cancelled.?

Can you share your code?