dev #1
1 changed files with 66 additions and 66 deletions
|
@ -9,74 +9,74 @@ export default async <T>(path: string, options: NitroFetchOptions<string> = {})
|
||||||
path = path.slice(0, path.lastIndexOf("/"));
|
path = path.slice(0, path.lastIndexOf("/"));
|
||||||
}
|
}
|
||||||
console.log("formatted path:", path);
|
console.log("formatted path:", path);
|
||||||
const accessToken = useCookie("access_token");
|
const accessToken = useCookie("access_token");
|
||||||
console.log("access token:", accessToken.value);
|
console.log("access token:", accessToken.value);
|
||||||
const apiBase = useCookie("api_base").value;
|
const apiBase = useCookie("api_base").value;
|
||||||
const apiVersion = useRuntimeConfig().public.apiVersion;
|
const apiVersion = useRuntimeConfig().public.apiVersion;
|
||||||
console.log("heyoooo")
|
console.log("heyoooo")
|
||||||
console.log("apiBase:", apiBase);
|
console.log("apiBase:", apiBase);
|
||||||
if (!apiBase) {
|
if (!apiBase) {
|
||||||
console.log("no api base");
|
console.log("no api base");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("path:", path)
|
console.log("path:", path)
|
||||||
const { revoke, refresh } = useAuth();
|
const { revoke, refresh } = useAuth();
|
||||||
console.log("access token 2:", accessToken.value);
|
console.log("access token 2:", accessToken.value);
|
||||||
|
|
||||||
let headers: HeadersInit = {};
|
let headers: HeadersInit = {};
|
||||||
|
|
||||||
if (accessToken.value) {
|
if (accessToken.value) {
|
||||||
headers = {
|
headers = {
|
||||||
...options.headers,
|
...options.headers,
|
||||||
"Authorization": `Bearer ${accessToken.value}`
|
"Authorization": `Bearer ${accessToken.value}`
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
headers = {
|
headers = {
|
||||||
...options.headers
|
...options.headers
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let reauthFailed = false;
|
let reauthFailed = false;
|
||||||
while (!reauthFailed) {
|
while (!reauthFailed) {
|
||||||
try {
|
try {
|
||||||
console.log("fetching:", URL.parse(apiBase + path));
|
console.log("fetching:", URL.parse(apiBase + path));
|
||||||
const res = await $fetch<T>(URL.parse(apiBase + path)!.href, {
|
const res = await $fetch<T>(URL.parse(apiBase + path)!.href, {
|
||||||
...options,
|
...options,
|
||||||
headers,
|
headers,
|
||||||
credentials: "include"
|
credentials: "include"
|
||||||
});
|
});
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error("Error fetching resource");
|
console.error("Error fetching resource");
|
||||||
if (error?.response?.status === 401) {
|
if (error?.response?.status === 401) {
|
||||||
console.log("Error status is 401");
|
console.log("Error status is 401");
|
||||||
if (!path.startsWith("/auth/refresh")) {
|
if (!path.startsWith("/auth/refresh")) {
|
||||||
console.log("Path is not refresh endpoint");
|
console.log("Path is not refresh endpoint");
|
||||||
try {
|
try {
|
||||||
console.log("Trying to refresh");
|
console.log("Trying to refresh");
|
||||||
await refresh();
|
await refresh();
|
||||||
console.log("Successfully refreshed token");
|
console.log("Successfully refreshed token");
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log("Failed to refresh token");
|
console.log("Failed to refresh token");
|
||||||
if (error?.response?.status === 401) {
|
if (error?.response?.status === 401) {
|
||||||
console.log("Refresh returned 401");
|
console.log("Refresh returned 401");
|
||||||
reauthFailed = true;
|
reauthFailed = true;
|
||||||
console.log("Revoking");
|
console.log("Revoking");
|
||||||
await revoke();
|
await revoke();
|
||||||
console.log("Redirecting to login");
|
console.log("Redirecting to login");
|
||||||
await navigateTo("/login");
|
await navigateTo("/login");
|
||||||
console.log("redirected");
|
console.log("redirected");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log("Path is refresh endpoint, throwing error");
|
console.log("Path is refresh endpoint, throwing error");
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log("throwing error");
|
console.log("throwing error");
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue