Skip to content

Commit

Permalink
Change CSRF error handling to open login URL in new tab when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
indigane committed Dec 20, 2024
1 parent ec03695 commit 348c73a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 12 additions & 1 deletion frontend/src/Router.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, {useEffect} from "react";
import {Navigate, Route, Routes} from "react-router-dom";
import App from "./app/App";
import {selectIsAuthenticated} from "./app/authSlice";
Expand Down Expand Up @@ -36,6 +36,13 @@ export default function Router() {
return isAuthenticated ? element : <Unauthorized />;
};

function WindowClose() {
useEffect(() => {
window.close();
}, []);
return null;
}

return (
<Routes>
<Route
Expand Down Expand Up @@ -148,6 +155,10 @@ export default function Router() {
path="logout"
element={isAuthenticated ? <></> : <Logout />}
/>
<Route
path="window-close"
element={<WindowClose />}
/>
<Route
index
element={<Navigate to="/housing-companies" />}
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/common/services/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ const baseQueryWithReAuth: BaseQueryFn<string | FetchArgs, unknown, FetchBaseQue
const errorMessage = (result.error?.data as {detail: string})?.detail;
if (errorMessage?.startsWith("CSRF Failed:")) {
hdsToast.error("CSRF Virhe. Uudelleenohjataan kirjautumissivulle.");
window.location.href = getSignInUrl(window.location.href);
setTimeout(() => {
// Open login in a new window/tab to avoid losing form data
const callBackUrl = new URL("/window-close", window.location.origin).toString();
window.open(getSignInUrl(callBackUrl), "_blank");
}, 1000);
}
}
return result;
Expand Down

0 comments on commit 348c73a

Please sign in to comment.