From 774e10d68cbe45840051adb0ae7be683cb83572d Mon Sep 17 00:00:00 2001 From: SauceyRed Date: Fri, 6 Jun 2025 01:57:19 +0200 Subject: [PATCH] fix: automatic auth refresh --- utils/fetchWithApi.ts | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/utils/fetchWithApi.ts b/utils/fetchWithApi.ts index 79fdae8..4089db8 100644 --- a/utils/fetchWithApi.ts +++ b/utils/fetchWithApi.ts @@ -9,8 +9,6 @@ export default async (path: string, options: NitroFetchOptions = {}) path = path.slice(0, path.lastIndexOf("/")); } console.log("formatted path:", path); - const accessToken = useCookie("access_token"); - console.log("access token:", accessToken.value); const apiBase = useCookie("api_base").value; const apiVersion = useRuntimeConfig().public.apiVersion; console.log("heyoooo") @@ -21,23 +19,24 @@ export default async (path: string, options: NitroFetchOptions = {}) } console.log("path:", path) const { revoke, refresh } = useAuth(); - console.log("access token 2:", accessToken.value); - + let headers: HeadersInit = {}; - - if (accessToken.value) { - headers = { - ...options.headers, - "Authorization": `Bearer ${accessToken.value}` - }; - } else { - headers = { - ...options.headers - }; - } - + + let reauthFailed = false; while (!reauthFailed) { + const accessToken = useCookie("access_token"); + console.log("access token:", accessToken.value); + if (accessToken.value) { + headers = { + ...options.headers, + "Authorization": `Bearer ${accessToken.value}` + }; + } else { + headers = { + ...options.headers + }; + } try { console.log("fetching:", URL.parse(apiBase + path)); const res = await $fetch(URL.parse(apiBase + path)!.href, { @@ -74,9 +73,10 @@ export default async (path: string, options: NitroFetchOptions = {}) console.log("Path is refresh endpoint, throwing error"); throw error; } + } else { + console.log("throwing error:", error); + throw error; } - console.log("throwing error"); - throw error; } } }