mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21:02 +00:00
26 lines
717 B
JavaScript
26 lines
717 B
JavaScript
import React, { useEffect } from "react";
|
|
import { Route } from "react-router-dom";
|
|
import { useAuth0 } from "../react-auth0-spa";
|
|
|
|
const PrivateRoute = ({ component: Component, path, ...rest }) => {
|
|
const { loading, isAuthenticated, loginWithRedirect } = useAuth0();
|
|
|
|
useEffect(() => {
|
|
if (loading || isAuthenticated) {
|
|
return;
|
|
}
|
|
const fn = async () => {
|
|
await loginWithRedirect({
|
|
appState: { targetUrl: path }
|
|
});
|
|
};
|
|
fn();
|
|
}, [loading, isAuthenticated, loginWithRedirect, path]);
|
|
|
|
const render = props =>
|
|
isAuthenticated === true ? <Component {...props} /> : null;
|
|
|
|
return <Route path={path} render={render} {...rest} />;
|
|
};
|
|
|
|
export default PrivateRoute;
|