I was working with the new auth0-spa and followed its tutorial to implement the auth0 SDK given by auth0 here. I want to automatically redirect to the auth0 login page instead of a page that gives a button saying Log In. Can anyone help me? Here is my code
import PrivateRoute from "./PrivateRoute";
import React,{useState} from "react";
import { useAuth0 } from "../react-auth0-spa";
import {BrowserRouter, HashRouter, Link, Route, Router, Switch} from "react-router-dom";
import DefaultLayout from "../containers/DefaultLayout";
import history from "../utils/history";
const NavBar = () => {
const { isAuthenticated, loginWithRedirect, logout } = useAuth0();
return (
<div>
{!isAuthenticated && (
<button id="button" onClick={() => loginWithRedirect({})}>Log in</button>
)}
{isAuthenticated && (
<HashRouter history = {history}>
<Switch>
<PrivateRoute path="/" component={DefaultLayout}/>
</Switch>
</HashRouter>
)}
</div>
);
};
export default NavBar;
This is what I did:
const App: React.FC<{}> = () => {
const { isLoading, loginWithRedirect, user } = useAuth0();
useEffect(() => {
(async function login() {
if (!isLoading && !user) {
await loginWithRedirect();
}
})();
}, [isLoading]);
return (
<React.Fragment>
{/* your code */}
</React.Fragment>
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With