From 932df18282daf62eee14bb11ca8f43e7e510f7b2 Mon Sep 17 00:00:00 2001 From: KIM_DEAHO <102588838+DHowor1d@users.noreply.github.com> Date: Tue, 25 Nov 2025 15:28:10 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=95=8C=EB=A6=BC=20=EC=97=90=EB=9F=AC?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/shared/providers/AlertProvider.tsx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/shared/providers/AlertProvider.tsx b/src/shared/providers/AlertProvider.tsx index 1c06c64..f61db6f 100644 --- a/src/shared/providers/AlertProvider.tsx +++ b/src/shared/providers/AlertProvider.tsx @@ -2,17 +2,29 @@ import { type PropsWithChildren, useEffect, useRef } from 'react'; import { createAlertSSE } from '@/api/sseClient'; import type { Alert } from '@/api/alertApi'; import { useAlertStore } from '@/shared/store/useAlertStore'; +import { useAuthStore } from '@/domains/login/store/useAuthStore'; const AlertProvider = ({ children }: PropsWithChildren): JSX.Element => { const addAlert = useAlertStore((state) => state.addAlert); const connectionRef = useRef | null>(null); const addAlertRef = useRef(addAlert); + const accessToken = useAuthStore((state) => state.accessToken); + const authenticated = useAuthStore((state) => state.authenticated); useEffect(() => { addAlertRef.current = addAlert; }, [addAlert]); useEffect(() => { + // 토큰이 없거나 인증되지 않은 상태에서는 SSE를 유지하지 않음 + if (!authenticated || !accessToken) { + if (connectionRef.current) { + connectionRef.current.close(); + connectionRef.current = null; + } + return; + } + const connection = createAlertSSE({ onMessage: (alert) => { addAlertRef.current(alert); @@ -31,7 +43,7 @@ const AlertProvider = ({ children }: PropsWithChildren): JSX.Element => { connection.close(); connectionRef.current = null; }; - }, []); + }, [accessToken, authenticated]); return <>{children}; };