[사용자 정보 및 로그아웃 처리 개선] 사용자 이름 접근 방식 수정 및 로그아웃 함수 비동기 처리 추가

This commit is contained in:
2025-08-28 16:47:24 +09:00
parent 9eb6e23757
commit 285a2662b7
3 changed files with 18 additions and 24 deletions

View File

@@ -1,5 +1,4 @@
import { useRuntimeConfig } from "#imports";
import { useUserStore } from "~/stores/user";
export const useApi = async <T>(
path: string,
@@ -8,11 +7,10 @@ export const useApi = async <T>(
body?: any;
query?: Record<string, any>;
headers?: HeadersInit;
credentials?: RequestCredentials;
} = {}
): Promise<T> => {
const userStore = useUserStore();
const config = useRuntimeConfig();
const method = options.method ? options.method.toUpperCase() : "GET";
try {
@@ -22,18 +20,11 @@ export const useApi = async <T>(
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);
}
},
}
);