Authentication
Auth0

How to use Auth0 ?

Note: The onion template uses the firebase authentication system by default.

  • Step-1 : Sign up for an account on Auth0 (opens in a new tab). After that, get the client id and domain from the dashboard and put them in the .env file.

  • Step-2 : Open the src/hooks/useAuth.js file. Thereupon, import the AuthContext from src/contexts/auth0Context then pass it as an argument in the useContext hook.

  • Step-3 : Call the useAuth hook inside AuthGuard, GuestGuard components, Login, Register page etc.

Step 1


_10
REACT_APP_AUTH0_CLIENT_ID= SET YOUR CLIENT ID
_10
REACT_APP_AUTH0_DOMAIN= SET YOUR DOMAIN

Step 2


_10
import { AuthContext } from "contexts/auth0Context";
_10
_10
const useAuth = () => useContext(AuthContext);
_10
export default useAuth;

Step 3

login.jsx

_17
import useAuth from "hooks/useAuth";
_17
_17
const Login = () => {
_17
const { loginloginWithPopup } = useAuth();
_17
_17
const onLogin = async () => {
_17
await loginWithPopup();
_17
};
_17
_17
return (
_17
<Box>
_17
<Button onClick={onLogin}>Login</Button>
_17
</Box>
_17
);
_17
};
_17
_17
export default Login;

profile.jsx

_18
import useAuth from "hooks/useAuth";
_18
_18
const Profile = () => {
_18
const { user, isAuthenticated, logout } = useAuth();
_18
_18
if (isAuthenticated && user) {
_18
return (
_18
<Box>
_18
<Typography>Email : {user.email}</Typography>
_18
<Button onClick={logout}>logout</Button>
_18
</Box>
_18
);
_18
}
_18
_18
return <Redirect to="/login" />;
_18
};
_18
_18
export default Profile;

How to delete from app ?

  • Delete the auth0Context.jsx file from src/contexts folder
  • You should also delete the AuthContext file from the src/hooks/useAuth.js file if you use this.
  • Also delete the REACT_APP_AUTH0_CLIENT_ID & REACT_APP_AUTH0_DOMAIN from .env file.
  • Uninstall dependecies @auth0/auth0-spa-js (opens in a new tab)

Reference