login with api
This commit is contained in:
parent
d8a9909eb9
commit
ff5b189b2f
16 changed files with 1241 additions and 66 deletions
34
frontend_management/composables/useApi.ts
Normal file
34
frontend_management/composables/useApi.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import type { UseFetchOptions } from 'nuxt/app';
|
||||
|
||||
export const useApi = () => {
|
||||
const config = useRuntimeConfig();
|
||||
const baseURL = config.public.apiBaseUrl as string;
|
||||
|
||||
const apiFetch = <T>(url: string, options?: UseFetchOptions<T>) => {
|
||||
return $fetch<T>(url, {
|
||||
baseURL,
|
||||
...options,
|
||||
headers: {
|
||||
...options?.headers,
|
||||
},
|
||||
onRequest({ options }) {
|
||||
// Add auth token if available
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
Authorization: `Bearer ${token}`
|
||||
};
|
||||
}
|
||||
},
|
||||
onResponseError({ response }) {
|
||||
// Handle errors globally
|
||||
console.error('API Error:', response.status, response._data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
apiFetch
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue