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

Change CSRF error handling to open login URL in new tab when necessary #519

Merged
merged 1 commit into from
Dec 20, 2024
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
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
Loading