Skip to content

Commit 5368f91

Browse files
adamjmcgrathlbalmaceda
authored andcommitted
Update PrivateRoute.js to match Quickstart
1 parent eb6f09d commit 5368f91

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

01-Login/src/components/PrivateRoute.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ import PropTypes from "prop-types";
33
import { Route, withRouter } from "react-router-dom";
44
import { useAuth0 } from "../react-auth0-spa";
55

6-
const PrivateRoute = ({ component: Component, path, location, ...rest }) => {
7-
const { isAuthenticated, loginWithRedirect } = useAuth0();
6+
const PrivateRoute = ({ component: Component, path, ...rest }) => {
7+
const { loading, isAuthenticated, loginWithRedirect } = useAuth0();
88

99
useEffect(() => {
10+
if (loading || isAuthenticated) {
11+
return;
12+
}
1013
const fn = async () => {
11-
if (!isAuthenticated) {
12-
await loginWithRedirect({
13-
appState: { targetUrl: location.pathname }
14-
});
15-
}
14+
await loginWithRedirect({
15+
appState: { targetUrl: path }
16+
});
1617
};
1718
fn();
18-
}, [isAuthenticated, loginWithRedirect, path, location]);
19+
}, [loading, isAuthenticated, loginWithRedirect, path]);
1920

2021
const render = props =>
2122
isAuthenticated === true ? <Component {...props} /> : null;
@@ -26,9 +27,6 @@ const PrivateRoute = ({ component: Component, path, location, ...rest }) => {
2627
PrivateRoute.propTypes = {
2728
component: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
2829
.isRequired,
29-
location: PropTypes.shape({
30-
pathname: PropTypes.string.isRequired
31-
}).isRequired,
3230
path: PropTypes.oneOfType([
3331
PropTypes.string,
3432
PropTypes.arrayOf(PropTypes.string)

02-Calling-an-API/src/components/PrivateRoute.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ import PropTypes from "prop-types";
33
import { Route, withRouter } from "react-router-dom";
44
import { useAuth0 } from "../react-auth0-spa";
55

6-
const PrivateRoute = ({ component: Component, path, location, ...rest }) => {
7-
const { isAuthenticated, loginWithRedirect } = useAuth0();
6+
const PrivateRoute = ({ component: Component, path, ...rest }) => {
7+
const { loading, isAuthenticated, loginWithRedirect } = useAuth0();
88

99
useEffect(() => {
10+
if (loading || isAuthenticated) {
11+
return;
12+
}
1013
const fn = async () => {
11-
if (!isAuthenticated) {
12-
await loginWithRedirect({
13-
appState: { targetUrl: location.pathname }
14-
});
15-
}
14+
await loginWithRedirect({
15+
appState: { targetUrl: path }
16+
});
1617
};
1718
fn();
18-
}, [isAuthenticated, loginWithRedirect, path, location]);
19+
}, [loading, isAuthenticated, loginWithRedirect, path]);
1920

2021
const render = props =>
2122
isAuthenticated === true ? <Component {...props} /> : null;
@@ -26,9 +27,6 @@ const PrivateRoute = ({ component: Component, path, location, ...rest }) => {
2627
PrivateRoute.propTypes = {
2728
component: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
2829
.isRequired,
29-
location: PropTypes.shape({
30-
pathname: PropTypes.string.isRequired
31-
}).isRequired,
3230
path: PropTypes.oneOfType([
3331
PropTypes.string,
3432
PropTypes.arrayOf(PropTypes.string)

0 commit comments

Comments
 (0)