From 4bfa2af213eaef208635da654829538e224df0a0 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Fri, 30 Oct 2026 15:21:34 +0900 Subject: [PATCH] =?UTF-8?q?261030=5F01=5FFIX:=20=EC=9D=B8=EC=A6=9D=20?= =?UTF-8?q?=EC=A0=84=ED=99=98=20=EC=8B=9C=20=EC=83=81=ED=83=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC=20=EB=B0=8F=20=ED=83=AD=20=EB=8F=99=EA=B8=B0=ED=99=94?= =?UTF-8?q?,=20=EC=99=B8=EB=B6=80=20=EB=A7=81=ED=81=AC=20=EA=B6=8C?= =?UTF-8?q?=ED=95=9C=20=EC=98=A4=EB=A5=98=20=EC=8B=9C=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=95=84=EC=9B=83=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/project/ExternalEntry.tsx | 17 +++++++----- src/components/providers/AppProvider.tsx | 3 ++ src/hooks/useResetSession.ts | 17 ++++++++++++ src/lib/auth-change.ts | 35 ++++++++++++++++++++++++ src/lib/http-client.ts | 6 ++-- src/query/auth.ts | 24 ++++++++-------- 6 files changed, 80 insertions(+), 22 deletions(-) create mode 100644 src/hooks/useResetSession.ts create mode 100644 src/lib/auth-change.ts diff --git a/src/components/project/ExternalEntry.tsx b/src/components/project/ExternalEntry.tsx index 8026037..91ed43e 100644 --- a/src/components/project/ExternalEntry.tsx +++ b/src/components/project/ExternalEntry.tsx @@ -4,9 +4,10 @@ import { useEffect, useRef, useState } from "react"; import { useRouter } from "next/navigation"; import type { Route } from "next"; import { useSetAtom } from "jotai"; -import { useQueryClient } from "@tanstack/react-query"; import { apiError } from "@kri/common/http"; import { useAuth } from "@/hooks/useAuth"; +import { useResetSession } from "@/hooks/useResetSession"; +import { notifyAuthChange } from "@/lib/auth-change"; import { selectedProjectAtom } from "@/hooks/useSelectedProject"; import { nextApi } from "@/lib/http-client"; import { isExternalSession, reportUrl, type ExternalSession } from "@/types/share"; @@ -16,7 +17,7 @@ export function ExternalEntry({ urlKey }: { urlKey: string }) { const router = useRouter(); const { hydrated, setAuth, logout } = useAuth(); const setProject = useSetAtom(selectedProjectAtom); - const queryClient = useQueryClient(); + const resetSession = useResetSession(); const pending = useRef | null>(null); const [error, setError] = useState(""); useEffect(() => { @@ -24,9 +25,7 @@ export function ExternalEntry({ urlKey }: { urlKey: string }) { let active = true; const enter = async () => { logout(); - setProject(null); - await queryClient.cancelQueries(); - queryClient.clear(); + await resetSession(); const result = await nextApi.post(`/external/${encodeURIComponent(urlKey)}/login`, null, { skipUnauthorizedHandler: true, skipForbiddenHandler: true, skipResponseErrorHandler: true, }); @@ -39,12 +38,16 @@ export function ExternalEntry({ urlKey }: { urlKey: string }) { if (!active) return; setProject({ userId: result.user.userId, project: { projectType: result.link.projectType, pkey: result.link.pkey } }); setAuth({ user: result.user, external: result.link }); + notifyAuthChange(result.link.realUrl); router.replace(reportUrl(result.link.realUrl) as Route); }).catch((reason: unknown) => { - if (active) setError(apiError(reason, { fallbackMessage: "공유 링크를 확인해 주세요." }).message); + if (active) { + notifyAuthChange("/login"); + setError(apiError(reason, { fallbackMessage: "공유 링크를 확인해 주세요." }).message); + } }); return () => { active = false; }; - }, [hydrated, logout, setAuth, setProject, queryClient, router, urlKey]); + }, [hydrated, logout, setAuth, setProject, resetSession, router, urlKey]); if (error) return

{error}

; return ; } diff --git a/src/components/providers/AppProvider.tsx b/src/components/providers/AppProvider.tsx index 354d28d..1fcfc28 100644 --- a/src/components/providers/AppProvider.tsx +++ b/src/components/providers/AppProvider.tsx @@ -1,11 +1,14 @@ "use client"; +import { useEffect } from "react"; import { Provider as JotaiProvider } from "jotai"; import { QueryProvider } from "@kri/common/query"; import { LoadingOverlayAdapter } from "@/components/app-shell/LoadingOverlayAdapter"; import { QueryLoadingSync } from "./QueryLoadingSync"; +import { subscribeToAuthChanges } from "@/lib/auth-change"; export function AppProvider({ children }: { children: React.ReactNode }) { + useEffect(() => subscribeToAuthChanges(), []); return ( diff --git a/src/hooks/useResetSession.ts b/src/hooks/useResetSession.ts new file mode 100644 index 0000000..d8f3159 --- /dev/null +++ b/src/hooks/useResetSession.ts @@ -0,0 +1,17 @@ +"use client"; + +import { useCallback } from "react"; +import { useSetAtom } from "jotai"; +import { useQueryClient } from "@tanstack/react-query"; +import { selectedProjectAtom } from "@/hooks/useSelectedProject"; + +/** Clear cached queries and the selected project before applying a new identity. */ +export function useResetSession() { + const queryClient = useQueryClient(); + const setProject = useSetAtom(selectedProjectAtom); + return useCallback(async () => { + await queryClient.cancelQueries(); + queryClient.clear(); + setProject(null); + }, [queryClient, setProject]); +} diff --git a/src/lib/auth-change.ts b/src/lib/auth-change.ts new file mode 100644 index 0000000..d240bff --- /dev/null +++ b/src/lib/auth-change.ts @@ -0,0 +1,35 @@ +import { reportUrl } from "@/types/share"; + +// Set false to disable only cross-tab notifications and their listener. +const AUTH_CHANGE_SYNC_ENABLED = true; +const CHANNEL = "kart:auth-change"; +let channel: BroadcastChannel | undefined; + +function authChannel() { + if (!AUTH_CHANGE_SYNC_ENABLED || typeof BroadcastChannel === "undefined") return; + try { + return channel ??= new BroadcastChannel(CHANNEL); + } catch { + // Cross-tab sync is optional when the browser disallows messaging. + return undefined; + } +} + +export function notifyAuthChange(path: string) { + authChannel()?.postMessage(path); +} + +export function subscribeToAuthChanges() { + const listener = authChannel(); + if (!listener) return; + listener.onmessage = (event: MessageEvent) => { + const path = event.data === "/" || event.data === "/login" + ? event.data : reportUrl(event.data); + // A full navigation discards this tab's cache/state without clearing the new cookie. + if (path) window.location.replace(path); + }; + return () => { + listener.close(); + channel = undefined; + }; +} diff --git a/src/lib/http-client.ts b/src/lib/http-client.ts index 95da020..49d8184 100644 --- a/src/lib/http-client.ts +++ b/src/lib/http-client.ts @@ -1,4 +1,4 @@ -import { createHttpClient } from "@kri/common/http"; +import { apiError, createHttpClient, redirectToLogout } from "@kri/common/http"; import { clientEnv } from "@/lib/env.client"; const resolveBaseUrl = () => @@ -7,6 +7,8 @@ const resolveBaseUrl = () => : new URL(clientEnv.API_BASE, window.location.origin).toString(); export const nextApi = createHttpClient(resolveBaseUrl(), { - onForbidden: false, + onForbidden: (error) => { + if (apiError(error).code === "EXTERNAL_SCOPE_DENIED") redirectToLogout(); + }, onResponseError: false, }); diff --git a/src/query/auth.ts b/src/query/auth.ts index 7e36dc9..2c51db5 100644 --- a/src/query/auth.ts +++ b/src/query/auth.ts @@ -1,17 +1,16 @@ "use client"; import type { LoginRequest } from "@kri/common"; -import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { useSetAtom } from "jotai"; +import { useMutation } from "@tanstack/react-query"; import { useAuth } from "@/hooks/useAuth"; -import { selectedProjectAtom } from "@/hooks/useSelectedProject"; +import { useResetSession } from "@/hooks/useResetSession"; +import { notifyAuthChange } from "@/lib/auth-change"; import { nextApi } from "@/lib/http-client"; import { isLoginUser } from "@/types/kart"; export function useLoginMutation() { const { setFromLoginResponse } = useAuth(); - const setSelectedProject = useSetAtom(selectedProjectAtom); - const queryClient = useQueryClient(); + const resetSession = useResetSession(); return useMutation({ mutationFn: async (request: LoginRequest) => { const user = await nextApi.post("/login", request, { @@ -22,18 +21,17 @@ export function useLoginMutation() { if (!isLoginUser(user)) throw new Error("로그인 응답을 확인해 주세요."); return user; }, - onSuccess: (user) => { - queryClient.clear(); + onSuccess: async (user) => { // Legacy Login.js starts every session without a selected project. - setSelectedProject(null); + await resetSession(); setFromLoginResponse(user); + notifyAuthChange("/"); }, }); } export function useLogoutMutation() { - const queryClient = useQueryClient(); const { logout } = useAuth(); - const setSelectedProject = useSetAtom(selectedProjectAtom); + const resetSession = useResetSession(); return useMutation({ mutationFn: () => nextApi.post("/logout", null, { @@ -41,10 +39,10 @@ export function useLogoutMutation() { skipForbiddenHandler: true, skipResponseErrorHandler: true, }), - onSettled: () => { + onSettled: async () => { + await resetSession(); logout(); - setSelectedProject(null); - queryClient.clear(); + notifyAuthChange("/login"); }, }); } -- 2.49.0.windows.1