login pinia
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useFetch, useRuntimeConfig, useCookie } from '#imports'
|
||||
import { useFetch, useRuntimeConfig } from '#imports';
|
||||
|
||||
export const useApi = <T>(
|
||||
path: string,
|
||||
@@ -10,18 +10,23 @@ export const useApi = <T>(
|
||||
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<T>(() => `${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 // ← 이 줄 추가!
|
||||
})
|
||||
|
Reference in New Issue
Block a user