-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPrivateRoute.tsx
More file actions
35 lines (28 loc) · 869 Bytes
/
Copy pathPrivateRoute.tsx
File metadata and controls
35 lines (28 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import * as React from 'react';
import { ComponentType } from 'react';
import { useLocation } from 'react-router-dom';
import { useAuth0 } from './Auth0Wrapper';
interface PrivateRouteProps {
component: ComponentType;
path: string;
}
const PrivateRoute = ({ component: Component, path }: PrivateRouteProps) => {
const { isLoggingIn, isAuthenticated, loginWithRedirect } = useAuth0();
const location = useLocation();
React.useEffect(
() => {
if (isLoggingIn || isAuthenticated) {
return;
}
const login = async () => {
await loginWithRedirect({
appState: { targetUrl: location.pathname || path },
});
};
login();
},
[isLoggingIn, isAuthenticated, loginWithRedirect, location.pathname, path],
);
return isAuthenticated ? <Component /> : null;
};
export default PrivateRoute;