feat: add utility function to fetch with authrorization header

This commit is contained in:
SauceyRed 2025-05-06 00:55:46 +02:00
parent 8ae492a9f1
commit f04c88b392

23
utils/fetchWithAuth.ts Normal file
View file

@ -0,0 +1,23 @@
import type { NitroFetchRequest, NitroFetchOptions } from "nitropack";
export default async <T>(request: NitroFetchRequest, options: NitroFetchOptions<string> = {}) => {
const accessToken = useCookie("access_token");
console.log("access token 2:", accessToken.value);
try {
const res = await $fetch<T>(request, {
...options,
headers: {
...options.headers,
"Authorization": `Bearer ${accessToken.value}`
}
});
return res;
} catch (error: any) {
if (error?.response?.status === 401) {
// auth.revoke();
}
throw error;
}
}