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

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

@@ -33,7 +33,7 @@
<!-- 사용자 정보 드롭다운 --> <!-- 사용자 정보 드롭다운 -->
<div class="user-menu-wrapper"> <div class="user-menu-wrapper">
<div class="user-info" @click="toggleDropdown"> <div class="user-info" @click="toggleDropdown">
<span class="user-name">{{ userStore.user?.name }}</span> <span class="user-name">{{ userStore.name }}</span>
<div class="user-icon"> <div class="user-icon">
<svg <svg
width="24" width="24"
@@ -105,9 +105,9 @@ function handleClickOutside(event) {
} }
} }
function logout() { async function logout() {
userStore.logout();
showDropdown.value = false; showDropdown.value = false;
await userStore.logout();
router.push("/login"); router.push("/login");
} }

View File

@@ -1,5 +1,4 @@
import { useRuntimeConfig } from "#imports"; import { useRuntimeConfig } from "#imports";
import { useUserStore } from "~/stores/user";
export const useApi = async <T>( export const useApi = async <T>(
path: string, path: string,
@@ -8,11 +7,10 @@ export const useApi = async <T>(
body?: any; body?: any;
query?: Record<string, any>; query?: Record<string, any>;
headers?: HeadersInit; headers?: HeadersInit;
credentials?: RequestCredentials;
} = {} } = {}
): Promise<T> => { ): Promise<T> => {
const userStore = useUserStore();
const config = useRuntimeConfig(); const config = useRuntimeConfig();
const method = options.method ? options.method.toUpperCase() : "GET"; const method = options.method ? options.method.toUpperCase() : "GET";
try { try {
@@ -22,18 +20,11 @@ export const useApi = async <T>(
method: method as any, method: method as any,
body: options.body, body: options.body,
query: options.query, query: options.query,
credentials: options.credentials || "include", // 쿠키 자동 전송
headers: { headers: {
Authorization: "Bearer " + userStore.token, "Content-Type": "application/json",
...options.headers, ...options.headers,
}, },
onResponse({ response }) {
const authHeader = response.headers.get("Authorization");
if (authHeader && authHeader.startsWith("Bearer ")) {
const accessToken = authHeader.substring(7);
userStore.setToken(accessToken);
}
},
} }
); );

View File

@@ -24,7 +24,6 @@ export const useUserStore = defineStore("user", () => {
}); });
if (success) { if (success) {
console.log(data);
user.value = data; user.value = data;
isLoggedIn.value = true; isLoggedIn.value = true;
} else { } else {
@@ -46,14 +45,18 @@ export const useUserStore = defineStore("user", () => {
} }
}; };
const logout = () => { const logout = async () => {
try {
await useApi("/members/logout", {
method: "post",
});
} catch (error) {
console.error("로그아웃 요청 실패:", error);
} finally {
// 로컬 상태 정리
user.value = null; user.value = null;
token.value = null;
isLoggedIn.value = false; isLoggedIn.value = false;
}
// 로컬 스토리지에서 제거
localStorage.removeItem("user");
localStorage.removeItem("token");
}; };
const checkAuth = () => { const checkAuth = () => {