Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect to Original URL After Session Expiry and Re-login #6495

Merged
merged 16 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading