|
1 | | -import React, { useEffect } from "react"; |
2 | | -import PropTypes from "prop-types"; |
3 | | -import { Route } from "react-router-dom"; |
4 | | -import { useAuth0 } from "../react-auth0-spa"; |
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { Route, withRouter } from 'react-router-dom'; |
| 4 | +import { useAuth0 } from '../react-auth0-spa'; |
5 | 5 |
|
6 | | -const PrivateRoute = ({ component: Component, path, ...rest }) => { |
| 6 | +const PrivateRoute = ({ component: Component, path, location, ...rest }) => { |
7 | 7 | const { isAuthenticated, loginWithRedirect } = useAuth0(); |
8 | 8 |
|
9 | 9 | useEffect(() => { |
10 | 10 | const fn = async () => { |
11 | 11 | if (!isAuthenticated) { |
12 | 12 | await loginWithRedirect({ |
13 | | - appState: { targetUrl: path } |
| 13 | + appState: { targetUrl: location.pathname } |
14 | 14 | }); |
15 | 15 | } |
16 | 16 | }; |
17 | 17 | fn(); |
18 | | - }, [isAuthenticated, loginWithRedirect, path]); |
| 18 | + }, [isAuthenticated, loginWithRedirect, path, location]); |
19 | 19 |
|
20 | 20 | const render = props => |
21 | | - isAuthenticated === true ? <Component {...props} /> : null; |
| 21 | + isAuthenticated === true ? <Component {...props} /> : null; |
22 | 22 |
|
23 | 23 | return <Route path={path} render={render} {...rest} />; |
24 | 24 | }; |
25 | 25 |
|
26 | 26 | PrivateRoute.propTypes = { |
27 | 27 | component: PropTypes.oneOfType([PropTypes.element, PropTypes.func]) |
28 | | - .isRequired, |
| 28 | + .isRequired, |
| 29 | + location: PropTypes.shape({ |
| 30 | + pathName: PropTypes.string.isRequired |
| 31 | + }).isRequired, |
29 | 32 | path: PropTypes.oneOfType([ |
30 | 33 | PropTypes.string, |
31 | 34 | PropTypes.arrayOf(PropTypes.string) |
32 | 35 | ]).isRequired |
33 | 36 | }; |
34 | 37 |
|
35 | | -export default PrivateRoute; |
| 38 | +export default withRouter(PrivateRoute); |
0 commit comments