diff --git a/utils/fetchWithAuth.ts b/utils/fetchWithAuth.ts new file mode 100644 index 0000000..5a3bfaf --- /dev/null +++ b/utils/fetchWithAuth.ts @@ -0,0 +1,23 @@ +import type { NitroFetchRequest, NitroFetchOptions } from "nitropack"; + +export default async (request: NitroFetchRequest, options: NitroFetchOptions = {}) => { + const accessToken = useCookie("access_token"); + console.log("access token 2:", accessToken.value); + + try { + const res = await $fetch(request, { + ...options, + headers: { + ...options.headers, + "Authorization": `Bearer ${accessToken.value}` + } + }); + + return res; + } catch (error: any) { + if (error?.response?.status === 401) { + // auth.revoke(); + } + throw error; + } +}