diff --git a/composables/useApi.ts b/composables/useApi.ts index e0767d5..9194c08 100644 --- a/composables/useApi.ts +++ b/composables/useApi.ts @@ -1,4 +1,4 @@ -import { useFetch, useRuntimeConfig, useCookie } from '#imports' +import { useFetch, useRuntimeConfig } from '#imports'; export const useApi = ( path: string, @@ -10,18 +10,23 @@ export const useApi = ( server?: boolean // ← 이 줄 추가! } = {} ) => { - const config = useRuntimeConfig() - const token = useCookie('token') + const userStore = useUserStore(); + + const config = useRuntimeConfig(); const method = options.method ? options.method.toUpperCase() : 'GET' - + return useFetch(() => `${config.public.apiBase}${config.public.contextPath}${path}`, { method: method as any, // 타입 강제 우회 body: options.body, query: options.query, headers: { - Authorization: token.value ? `Bearer ${token.value}` : '', - ...options.headers + Authorization: 'Bearer ' + userStore.getToken, + ...options.headers, + }, + onResponse({response}){ + const accessToken = response.headers.get("Authorization") || ""; + userStore.setToken(accessToken.replace("Bearer ", "")); }, server: options.server // ← 이 줄 추가! }) diff --git a/pages/[tabId]/index.vue b/pages/[tabId]/index.vue index 31fb64e..ec7dc4c 100644 --- a/pages/[tabId]/index.vue +++ b/pages/[tabId]/index.vue @@ -27,6 +27,9 @@ {{ userStore.isAdmin ? "관리자" : "사용자" }} 권한으로 로그인되었습니다.

+

+ +

{ method: 'post', body: { userId, password } }) - + let mockUser; if(data && data.value && data.value.success){ @@ -39,13 +39,9 @@ export const useUserStore = defineStore("user", () => { } user.value = mockUser; - token.value = "mock-token-" + Date.now(); + // token.value = "mock-token-" + Date.now(); isLoggedIn.value = true; - // 로컬 스토리지에 저장 - localStorage.setItem("user", JSON.stringify(mockUser)); - localStorage.setItem("token", token.value); - return { success: true, user: mockUser }; } catch (error) { console.error("로그인 실패:", error); @@ -79,6 +75,14 @@ export const useUserStore = defineStore("user", () => { } }; + const setToken = (accessToken : string) => { + token.value = accessToken; + } + + const getToken = () => { + return token; + } + // 초기 인증 상태 확인 if (import.meta.client) { checkAuth(); @@ -98,5 +102,7 @@ export const useUserStore = defineStore("user", () => { login, logout, checkAuth, + setToken, + getToken }; });