Compare commits

...

2 Commits

Author SHA1 Message Date
ec6212c02f Merge branch 'main' of https://demo.stam.kr/leejisun9/bio_frontend 2025-08-27 17:06:39 +09:00
ca44f8936a login pinia 2025-08-27 17:06:25 +09:00
3 changed files with 26 additions and 12 deletions

View File

@@ -1,4 +1,4 @@
import { useFetch, useRuntimeConfig, useCookie } from '#imports' import { useFetch, useRuntimeConfig } from '#imports';
export const useApi = <T>( export const useApi = <T>(
path: string, path: string,
@@ -10,18 +10,23 @@ export const useApi = <T>(
server?: boolean // ← 이 줄 추가! server?: boolean // ← 이 줄 추가!
} = {} } = {}
) => { ) => {
const config = useRuntimeConfig() const userStore = useUserStore();
const token = useCookie('token')
const config = useRuntimeConfig();
const method = options.method ? options.method.toUpperCase() : 'GET' const method = options.method ? options.method.toUpperCase() : 'GET'
return useFetch<T>(() => `${config.public.apiBase}${config.public.contextPath}${path}`, { return useFetch<T>(() => `${config.public.apiBase}${config.public.contextPath}${path}`, {
method: method as any, // 타입 강제 우회 method: method as any, // 타입 강제 우회
body: options.body, body: options.body,
query: options.query, query: options.query,
headers: { headers: {
Authorization: token.value ? `Bearer ${token.value}` : '', Authorization: 'Bearer ' + userStore.getToken,
...options.headers ...options.headers,
},
onResponse({response}){
const accessToken = response.headers.get("Authorization") || "";
userStore.setToken(accessToken.replace("Bearer ", ""));
}, },
server: options.server // ← 이 줄 추가! server: options.server // ← 이 줄 추가!
}) })

View File

@@ -27,6 +27,9 @@
{{ userStore.isAdmin ? "관리자" : "사용자" }} 권한으로 {{ userStore.isAdmin ? "관리자" : "사용자" }} 권한으로
로그인되었습니다. 로그인되었습니다.
</p> </p>
<p class="text-sm text-gray-600">
<button @click="useApi<ApiResponse<{}>>('/files/download/1756167537354001',{method: 'get'});" > Test</button>
</p>
</div> </div>
<div <div
v-else v-else

View File

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