mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-09 10:16:39 +00:00
24 lines
719 B
JavaScript
24 lines
719 B
JavaScript
import React, { useEffect } from "react";
|
|
import { Route } from "react-router-dom";
|
|
import { useAuth0 } from "../authentication/auth0";
|
|
|
|
export const PrivateRoute = ({ component: Component, path, ...rest }) => {
|
|
const { loading, isAuthenticated, loginWithRedirect } = useAuth0();
|
|
|
|
useEffect(() => {
|
|
if (loading || isAuthenticated) {
|
|
return;
|
|
}
|
|
const fn = async () => {
|
|
await loginWithRedirect({
|
|
appState: { targetUrl: window.location.pathname }
|
|
});
|
|
};
|
|
fn();
|
|
}, [loading, isAuthenticated, loginWithRedirect, path]);
|
|
|
|
const render = props =>
|
|
isAuthenticated === true ? <Component {...props} /> : null;
|
|
|
|
return <Route path={path} render={render} {...rest} />;
|
|
};
|