Skip to content

Commit 56555b3

Browse files
author
Kavita Chaudhry
committed
Fixes redirect on Login if you are using React Router
1 parent fee1809 commit 56555b3

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ node_modules
44
**/auth0-variables.js
55
npm-debug.log
66
.editorconfig
7+
.idea/workspace.xml
8+
.idea/vcs.xml
9+
.idea/encodings.xml
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
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';
55

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

99
useEffect(() => {
1010
const fn = async () => {
1111
if (!isAuthenticated) {
1212
await loginWithRedirect({
13-
appState: { targetUrl: path }
13+
appState: { targetUrl: location.pathname }
1414
});
1515
}
1616
};
1717
fn();
18-
}, [isAuthenticated, loginWithRedirect, path]);
18+
}, [isAuthenticated, loginWithRedirect, path, location]);
1919

2020
const render = props =>
21-
isAuthenticated === true ? <Component {...props} /> : null;
21+
isAuthenticated === true ? <Component {...props} /> : null;
2222

2323
return <Route path={path} render={render} {...rest} />;
2424
};
2525

2626
PrivateRoute.propTypes = {
2727
component: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
28-
.isRequired,
28+
.isRequired,
29+
location: PropTypes.shape({
30+
pathName: PropTypes.string.isRequired
31+
}).isRequired,
2932
path: PropTypes.oneOfType([
3033
PropTypes.string,
3134
PropTypes.arrayOf(PropTypes.string)
3235
]).isRequired
3336
};
3437

35-
export default PrivateRoute;
38+
export default withRouter(PrivateRoute);

0 commit comments

Comments
 (0)