feat: add utility function to fetch with authrorization header
This commit is contained in:
parent
8ae492a9f1
commit
f04c88b392
1 changed files with 23 additions and 0 deletions
23
utils/fetchWithAuth.ts
Normal file
23
utils/fetchWithAuth.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue