Skip to content

Commit

Permalink
Redirect to Original URL After Session Expiry and Re-login (#6495)
Browse files Browse the repository at this point in the history
* go back to previous url after session expiration

* add query parameters instead of localstorage

* resolve cross scripting

* use newURL instead of string manipulation

* check origin while redirecting

* remove cross-site-scripting

* convert the redirection into a function

* remove else redirection
  • Loading branch information
AshrafMd-1 authored Nov 6, 2023
1 parent e1eac85 commit e30d6bd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Components/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import CircularProgress from "../Common/components/CircularProgress";
import { LocalStorageKeys } from "../../Common/constants";
import ReactMarkdown from "react-markdown";
import rehypeRaw from "rehype-raw";
import { handleRedirection } from "../../Utils/utils";

export const Login = (props: { forgot?: boolean }) => {
const {
Expand Down Expand Up @@ -109,7 +110,7 @@ export const Login = (props: { forgot?: boolean }) => {
window.location.pathname === "/" ||
window.location.pathname === "/login"
) {
window.location.href = "/facility";
handleRedirection();
} else {
window.location.href = window.location.pathname.toString();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Redux/fireRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const fireRequest = (
if (error.response.status > 400 && error.response.status < 500) {
if (error.response.data && error.response.data.detail) {
if (error.response.data.code === "token_not_valid") {
window.location.href = "/session-expired";
window.location.href = `/session-expired?redirect=${window.location.href}`;
}
Notification.Error({
msg: error.response.data.detail,
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/request/handleResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function handleResponse(
if (res.status >= 400) {
// Invalid token
if (!silent && error?.code === "token_not_valid") {
navigate("/session-expired");
navigate(`/session-expired?redirect=${window.location.href}`);
}

notify?.Error({ msg: error?.detail || "Something went wrong...!" });
Expand Down
25 changes: 24 additions & 1 deletion src/Utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,31 @@ export const handleSignOut = (forceReload: boolean) => {
Object.values(LocalStorageKeys).forEach((key) =>
localStorage.removeItem(key)
);
const redirectURL = new URLSearchParams(window.location.search).get(
"redirect"
);
redirectURL ? navigate(`/?redirect=${redirectURL}`) : navigate("/");
if (forceReload) window.location.href = "/";
else navigate("/");
};

export const handleRedirection = () => {
const redirectParam = new URLSearchParams(window.location.search).get(
"redirect"
);
try {
if (redirectParam) {
const redirectURL = new URL(redirectParam);

if (redirectURL.origin === window.location.origin) {
const newPath = redirectURL.pathname + redirectURL.search;
window.location.href = `${window.location.origin}${newPath}`;
return;
}
}
window.location.href = "/facility";
} catch {
window.location.href = "/facility";
}
};

/**
Expand Down

0 comments on commit e30d6bd

Please sign in to comment.