diff --git a/components/layout/AppHeader.vue b/components/layout/AppHeader.vue index a84fe33..d942bc2 100644 --- a/components/layout/AppHeader.vue +++ b/components/layout/AppHeader.vue @@ -33,7 +33,7 @@
- {{ userStore.user?.name }} + {{ userStore.name }}
( path: string, @@ -8,11 +7,10 @@ export const useApi = async ( body?: any; query?: Record; headers?: HeadersInit; + credentials?: RequestCredentials; } = {} ): Promise => { - const userStore = useUserStore(); const config = useRuntimeConfig(); - const method = options.method ? options.method.toUpperCase() : "GET"; try { @@ -22,18 +20,11 @@ export const useApi = async ( method: method as any, body: options.body, query: options.query, + credentials: options.credentials || "include", // 쿠키 자동 전송 headers: { - Authorization: "Bearer " + userStore.token, + "Content-Type": "application/json", ...options.headers, }, - onResponse({ response }) { - const authHeader = response.headers.get("Authorization"); - - if (authHeader && authHeader.startsWith("Bearer ")) { - const accessToken = authHeader.substring(7); - userStore.setToken(accessToken); - } - }, } ); diff --git a/stores/user.ts b/stores/user.ts index 9eb8484..5695f2c 100644 --- a/stores/user.ts +++ b/stores/user.ts @@ -24,7 +24,6 @@ export const useUserStore = defineStore("user", () => { }); if (success) { - console.log(data); user.value = data; isLoggedIn.value = true; } else { @@ -46,14 +45,18 @@ export const useUserStore = defineStore("user", () => { } }; - const logout = () => { - user.value = null; - token.value = null; - isLoggedIn.value = false; - - // 로컬 스토리지에서 제거 - localStorage.removeItem("user"); - localStorage.removeItem("token"); + const logout = async () => { + try { + await useApi("/members/logout", { + method: "post", + }); + } catch (error) { + console.error("로그아웃 요청 실패:", error); + } finally { + // 로컬 상태 정리 + user.value = null; + isLoggedIn.value = false; + } }; const checkAuth = () => {