mirror of
https://github.com/rjNemo/meal_planner
synced 2026-06-06 02:26:49 +00:00
25 lines
754 B
JavaScript
25 lines
754 B
JavaScript
import React, { useEffect } from "react";
|
|
import { Route } from "react-router-dom";
|
|
import { useAuth0 } from "../utils/auth0-spa";
|
|
|
|
export const PrivateRoute = ({ component: Component, path, ...rest }) => {
|
|
const { loading, isAuthenticated, loginWithRedirect } = useAuth0();
|
|
|
|
useEffect(() => {
|
|
if (loading || isAuthenticated) {
|
|
// catches infinite loading when no user is logged in
|
|
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} />;
|
|
};
|