Fix the relative login redirects (#1872)

Signed-off-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
This commit is contained in:
Daniel Valdivia
2022-04-20 10:57:28 -07:00
committed by GitHub
parent af76280f1d
commit 74ba1c80a9
5 changed files with 17 additions and 7 deletions

View File

@@ -32,6 +32,7 @@ import useApi from "./screens/Console/Common/Hooks/useApi";
import { ErrorResponseHandler } from "./common/types";
import { ReplicationSite } from "./screens/Console/Configurations/SiteReplication/SiteReplication";
import { SRInfoStateType } from "./types";
import { baseUrl } from "./history";
interface ProtectedRouteProps {
loggedIn: boolean;
@@ -113,7 +114,11 @@ const ProtectedRoute = ({
return null;
}
// redirect user to the right page based on session status
return loggedIn ? <Component /> : <Redirect to={{ pathname: "login" }} />;
return loggedIn ? (
<Component />
) : (
<Redirect to={{ pathname: `${baseUrl}login` }} />
);
};
const mapState = (state: AppState) => ({

View File

@@ -18,6 +18,7 @@ import request from "superagent";
import get from "lodash/get";
import { clearSession } from "../utils";
import { ErrorResponseHandler } from "../types";
import { baseUrl } from "../../history";
export class API {
invoke(method: string, url: string, data?: object) {
@@ -37,7 +38,7 @@ export class API {
clearSession();
// Refresh the whole page to ensure cache is clear
// and we dont end on an infinite loop
window.location.href = "login";
window.location.href = `${baseUrl}login`;
return;
}
return this.onError(err);
@@ -71,7 +72,7 @@ export class API {
return Promise.reject(throwMessage);
} else {
clearSession();
window.location.href = "login";
window.location.href = `${baseUrl}login`;
}
}
}

View File

@@ -4,6 +4,9 @@ import { BrowserHistoryBuildOptions } from "history/createBrowserHistory";
let browserHistoryOpts: BrowserHistoryBuildOptions = {};
let basename = document.baseURI.replace(window.location.origin, "");
// check if we are using base path, if not this always is `/`
const baseLocation = new URL(document.baseURI);
export const baseUrl = baseLocation.pathname;
if (basename !== "") {
browserHistoryOpts.basename = basename;

View File

@@ -27,7 +27,7 @@ import { setMenuOpen, userLoggedIn } from "../../../actions";
import { ErrorResponseHandler } from "../../../common/types";
import { clearSession } from "../../../common/utils";
import history from "../../../history";
import history, { baseUrl } from "../../../history";
import api from "../../../common/api";
import { resetSession } from "../actions";
@@ -106,8 +106,9 @@ const Menu = ({
clearSession();
userLoggedIn(false);
localStorage.setItem("userLoggedIn", "");
localStorage.setItem("redirect-path", "");
resetSession();
history.push("login");
history.push(`${baseUrl}login`);
};
api
.invoke("POST", `/api/v1/logout`)

View File

@@ -19,7 +19,7 @@ import api from "../../common/api";
import withStyles from "@mui/styles/withStyles";
import { Theme } from "@mui/material/styles";
import createStyles from "@mui/styles/createStyles";
import history from "../../history";
import history, { baseUrl } from "../../history";
import { Paper } from "@mui/material";
import Grid from "@mui/material/Grid";
import Typography from "@mui/material/Typography";
@@ -175,7 +175,7 @@ const LoginCallback = ({ classes }: ILoginCallBackProps) => {
</Typography>
<Button
component={"a"}
href="login"
href={`${baseUrl}login`}
type="submit"
variant="contained"
color="primary"