fix(auth): centralize redirect path storage

This commit is contained in:
너이름 2026-05-11 06:30:26 +09:00
parent c8374e6c5b
commit fb0c13911d
3 changed files with 14 additions and 5 deletions

View file

@ -8,6 +8,7 @@ import { toast } from "sonner";
import { Logo } from "@/components/Logo"; import { Logo } from "@/components/Logo";
import { useGlobalLoadingEffect } from "@/hooks/use-global-loading"; import { useGlobalLoadingEffect } from "@/hooks/use-global-loading";
import { getAuthErrorDetails, shouldRetry } from "@/lib/auth-errors"; import { getAuthErrorDetails, shouldRetry } from "@/lib/auth-errors";
import { setRedirectPath } from "@/lib/auth-utils";
import { AUTH_TYPE } from "@/lib/env-config"; import { AUTH_TYPE } from "@/lib/env-config";
import { AmbientBackground } from "./AmbientBackground"; import { AmbientBackground } from "./AmbientBackground";
import { GoogleLoginButton } from "./GoogleLoginButton"; import { GoogleLoginButton } from "./GoogleLoginButton";
@ -33,7 +34,7 @@ function LoginContent() {
// Save returnUrl to localStorage so it persists through OAuth flows (e.g., Google) // Save returnUrl to localStorage so it persists through OAuth flows (e.g., Google)
// This is read by TokenHandler after successful authentication // This is read by TokenHandler after successful authentication
if (returnUrl) { if (returnUrl) {
localStorage.setItem("surfsense_redirect_path", decodeURIComponent(returnUrl)); setRedirectPath(decodeURIComponent(returnUrl));
} }
// Show registration success message // Show registration success message

View file

@ -31,7 +31,7 @@ import {
import { Spinner } from "@/components/ui/spinner"; import { Spinner } from "@/components/ui/spinner";
import type { AcceptInviteResponse } from "@/contracts/types/invites.types"; import type { AcceptInviteResponse } from "@/contracts/types/invites.types";
import { invitesApiService } from "@/lib/apis/invites-api.service"; import { invitesApiService } from "@/lib/apis/invites-api.service";
import { getBearerToken } from "@/lib/auth-utils"; import { getBearerToken, setRedirectPath } from "@/lib/auth-utils";
import { import {
trackSearchSpaceInviteAccepted, trackSearchSpaceInviteAccepted,
trackSearchSpaceInviteDeclined, trackSearchSpaceInviteDeclined,
@ -125,7 +125,7 @@ export default function InviteAcceptPage() {
// Store the invite code to redirect back after login // Store the invite code to redirect back after login
localStorage.setItem("pending_invite_code", inviteCode); localStorage.setItem("pending_invite_code", inviteCode);
// Save the current invite page URL so we can return after authentication // 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) // Redirect to login (we manually set the path above since invite pages need special handling)
window.location.href = "/login"; window.location.href = "/login";
}; };

View file

@ -60,12 +60,20 @@ export function handleUnauthorized(): void {
const currentPath = pathname + window.location.search + window.location.hash; const currentPath = pathname + window.location.search + window.location.hash;
const excludedPaths = ["/auth", "/auth/callback", "/"]; const excludedPaths = ["/auth", "/auth/callback", "/"];
if (!excludedPaths.includes(pathname)) { if (!excludedPaths.includes(pathname)) {
localStorage.setItem(REDIRECT_PATH_KEY, currentPath); setRedirectPath(currentPath);
} }
window.location.href = getLoginPath(); 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 * Gets the stored redirect path and clears it from storage
* Call this after successful login to redirect the user back * 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 // Don't save auth-related paths or home page
const excludedPaths = ["/auth", "/auth/callback", "/", "/login", "/register", "/desktop/login"]; const excludedPaths = ["/auth", "/auth/callback", "/", "/login", "/register", "/desktop/login"];
if (!excludedPaths.includes(window.location.pathname)) { if (!excludedPaths.includes(window.location.pathname)) {
localStorage.setItem(REDIRECT_PATH_KEY, currentPath); setRedirectPath(currentPath);
} }
window.location.href = getLoginPath(); window.location.href = getLoginPath();