|
15 | 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 | 16 |
|
17 | 17 | import React, { useEffect, useState } from "react";
|
18 |
| -import { Redirect } from "react-router-dom"; |
19 |
| - |
| 18 | +import { Redirect, useLocation } from "react-router-dom"; |
20 | 19 | import api from "./common/api";
|
21 | 20 | import { ISessionResponse } from "./screens/Console/types";
|
22 | 21 | import useApi from "./screens/Console/Common/Hooks/useApi";
|
@@ -48,6 +47,13 @@ const ProtectedRoute = ({ Component }: ProtectedRouteProps) => {
|
48 | 47 | const [sessionLoading, setSessionLoading] = useState<boolean>(true);
|
49 | 48 | const userLoggedIn = useSelector((state: AppState) => state.system.loggedIn);
|
50 | 49 |
|
| 50 | + const { pathname = "" } = useLocation(); |
| 51 | + |
| 52 | + const StorePathAndRedirect = () => { |
| 53 | + localStorage.setItem("redirect-path", pathname); |
| 54 | + return <Redirect to={{ pathname: `${baseUrl}login` }} />; |
| 55 | + }; |
| 56 | + |
51 | 57 | useEffect(() => {
|
52 | 58 | api
|
53 | 59 | .invoke("GET", `/api/v1/session`)
|
@@ -104,11 +110,7 @@ const ProtectedRoute = ({ Component }: ProtectedRouteProps) => {
|
104 | 110 | return null;
|
105 | 111 | }
|
106 | 112 | // redirect user to the right page based on session status
|
107 |
| - return userLoggedIn ? ( |
108 |
| - <Component /> |
109 |
| - ) : ( |
110 |
| - <Redirect to={{ pathname: `${baseUrl}login` }} /> |
111 |
| - ); |
| 113 | + return userLoggedIn ? <Component /> : <StorePathAndRedirect />; |
112 | 114 | };
|
113 | 115 |
|
114 | 116 | export default ProtectedRoute;
|
0 commit comments