Skip to content
4 changes: 0 additions & 4 deletions src/components/common/Header/ProfileMenuTrigger.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";

import { useQueryClient } from "@tanstack/react-query";
import Image from "next/image";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
Expand All @@ -11,7 +10,6 @@ import { useClickOutside } from "@/hooks/useClickOutside";
import type { AuthRole } from "@/lib/auth/role";
import { isPublicPath } from "@/lib/auth/redirect";
import { APP_ROUTES } from "@/lib/constants/appRoutes";
import { QUERY_KEYS } from "@/lib/constants/queryKeys";
import { cn } from "@/lib/utils/cn";
import { useAuthStore } from "@/stores/useAuthStore";

Expand All @@ -36,7 +34,6 @@ export default function ProfileMenuTrigger({
}: ProfileMenuTriggerProps) {
const pathname = usePathname();
const router = useRouter();
const queryClient = useQueryClient();
const logout = useAuthStore((state) => state.logout);

const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -109,7 +106,6 @@ export default function ProfileMenuTrigger({
const logoutPath = role === "MOVER" ? APP_ROUTES.MOVER_LOGIN : APP_ROUTES.LOGIN;

await logout();
queryClient.removeQueries({ queryKey: QUERY_KEYS.NOTIFICATIONS.ALL });

if (isPublicPage) {
router.refresh();
Expand Down
14 changes: 12 additions & 2 deletions src/providers/AppProviders.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
"use client";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { QueryClient, QueryClientProvider, useQueryClient } from "@tanstack/react-query";
import { useState, type ReactNode } from "react";

import { setAppQueryClient } from "@/providers/query/appQueryClient";
import { AuthProvider } from "@/providers/AuthProvider";

interface AppProvidersProps {
children: ReactNode;
}

/** Provider와 동일한 client를 브라우저 모듈에 동기 등록 */
const AppQueryClientRegistrar = ({ children }: { children: ReactNode }) => {
Comment thread
9g-g9 marked this conversation as resolved.
Resolved
const queryClient = useQueryClient();
setAppQueryClient(queryClient);
return children;
};

export const AppProviders = ({ children }: AppProvidersProps) => {
const [queryClient] = useState(
() =>
Expand All @@ -28,7 +36,9 @@ export const AppProviders = ({ children }: AppProvidersProps) => {

return (
<QueryClientProvider client={queryClient}>
<AuthProvider>{children}</AuthProvider>
<AppQueryClientRegistrar>
<AuthProvider>{children}</AuthProvider>
</AppQueryClientRegistrar>
</QueryClientProvider>
);
};
16 changes: 16 additions & 0 deletions src/providers/query/appQueryClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { QueryClient } from "@tanstack/react-query";

let appQueryClient: QueryClient | null = null;

/**
* AppProviders(AppQueryClientRegistrar)에서 Provider와 동일한 client를 등록합니다.
* useState 초기화 시점 set은 SSR에만 실행되므로 사용하지 않습니다.
*/
export const setAppQueryClient = (queryClient: QueryClient): void => {
appQueryClient = queryClient;
};

/** 로그아웃·세션 만료 시 사용자 데이터가 섞이지 않도록 캐시를 비웁니다. */
export const clearAppQueryCache = (): void => {
appQueryClient?.clear();
};
4 changes: 3 additions & 1 deletion src/stores/useAuthStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { isAuthPagePath, isOAuthCallbackPath } from "@/lib/auth/redirect";
import { clearNickname, loadNickname, saveNickname } from "@/lib/auth/nickname";
import { clearRole, loadRole, saveRole } from "@/lib/auth/role";
import { clearAuthTokens, getAccessToken } from "@/lib/auth/token";
import { clearAppQueryCache } from "@/providers/query/appQueryClient";
import { ApiError } from "@/types/api";

interface AuthState {
Expand Down Expand Up @@ -155,10 +156,11 @@ export const useAuthStore = create<AuthState>((set, get) => ({
clearAuthTokens();
clearNickname();
clearRole();
set({ ...UNAUTHENTICATED_STATE });
get().markUnauthenticated();
Comment thread
9g-g9 marked this conversation as resolved.
Resolved
},

markUnauthenticated: () => {
clearAppQueryCache();
set({ ...UNAUTHENTICATED_STATE });
},

Expand Down