From fb0c13911d191e60c40e69b3499dcb4de67b07cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=84=88=EC=9D=B4=EB=A6=84?= <너이메일> Date: Mon, 11 May 2026 06:30:26 +0900 Subject: [PATCH] fix(auth): centralize redirect path storage --- surfsense_web/app/(home)/login/page.tsx | 3 ++- surfsense_web/app/invite/[invite_code]/page.tsx | 4 ++-- surfsense_web/lib/auth-utils.ts | 12 ++++++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/surfsense_web/app/(home)/login/page.tsx b/surfsense_web/app/(home)/login/page.tsx index 3dbbf21a9..c336e757c 100644 --- a/surfsense_web/app/(home)/login/page.tsx +++ b/surfsense_web/app/(home)/login/page.tsx @@ -8,6 +8,7 @@ import { toast } from "sonner"; import { Logo } from "@/components/Logo"; import { useGlobalLoadingEffect } from "@/hooks/use-global-loading"; import { getAuthErrorDetails, shouldRetry } from "@/lib/auth-errors"; +import { setRedirectPath } from "@/lib/auth-utils"; import { AUTH_TYPE } from "@/lib/env-config"; import { AmbientBackground } from "./AmbientBackground"; import { GoogleLoginButton } from "./GoogleLoginButton"; @@ -33,7 +34,7 @@ function LoginContent() { // Save returnUrl to localStorage so it persists through OAuth flows (e.g., Google) // This is read by TokenHandler after successful authentication if (returnUrl) { - localStorage.setItem("surfsense_redirect_path", decodeURIComponent(returnUrl)); + setRedirectPath(decodeURIComponent(returnUrl)); } // Show registration success message diff --git a/surfsense_web/app/invite/[invite_code]/page.tsx b/surfsense_web/app/invite/[invite_code]/page.tsx index a778962b4..959a6d6d1 100644 --- a/surfsense_web/app/invite/[invite_code]/page.tsx +++ b/surfsense_web/app/invite/[invite_code]/page.tsx @@ -31,7 +31,7 @@ import { import { Spinner } from "@/components/ui/spinner"; import type { AcceptInviteResponse } from "@/contracts/types/invites.types"; import { invitesApiService } from "@/lib/apis/invites-api.service"; -import { getBearerToken } from "@/lib/auth-utils"; +import { getBearerToken, setRedirectPath } from "@/lib/auth-utils"; import { trackSearchSpaceInviteAccepted, trackSearchSpaceInviteDeclined, @@ -125,7 +125,7 @@ export default function InviteAcceptPage() { // Store the invite code to redirect back after login localStorage.setItem("pending_invite_code", inviteCode); // Save the current invite page URL so we can return after authentication - localStorage.setItem("surfsense_redirect_path", `/invite/${inviteCode}`); + setRedirectPath(`/invite/${inviteCode}`); // Redirect to login (we manually set the path above since invite pages need special handling) window.location.href = "/login"; }; diff --git a/surfsense_web/lib/auth-utils.ts b/surfsense_web/lib/auth-utils.ts index f0d4489ac..4848f4ab7 100644 --- a/surfsense_web/lib/auth-utils.ts +++ b/surfsense_web/lib/auth-utils.ts @@ -60,12 +60,20 @@ export function handleUnauthorized(): void { const currentPath = pathname + window.location.search + window.location.hash; const excludedPaths = ["/auth", "/auth/callback", "/"]; if (!excludedPaths.includes(pathname)) { - localStorage.setItem(REDIRECT_PATH_KEY, currentPath); + setRedirectPath(currentPath); } window.location.href = getLoginPath(); } } +/** + * Stores the path to redirect to after successful authentication. + */ +export function setRedirectPath(path: string): void { + if (typeof window === "undefined") return; + localStorage.setItem(REDIRECT_PATH_KEY, path); +} + /** * Gets the stored redirect path and clears it from storage * Call this after successful login to redirect the user back @@ -230,7 +238,7 @@ export function redirectToLogin(): void { // Don't save auth-related paths or home page const excludedPaths = ["/auth", "/auth/callback", "/", "/login", "/register", "/desktop/login"]; if (!excludedPaths.includes(window.location.pathname)) { - localStorage.setItem(REDIRECT_PATH_KEY, currentPath); + setRedirectPath(currentPath); } window.location.href = getLoginPath();