From f03867b9a7de3ab8adfc6d6d3c9bf868f441ceaa Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 1 Mar 2025 11:27:06 +0100 Subject: [PATCH] Add retry mechanism and limit for token requests --- src/components/repo/authv2.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/components/repo/authv2.ts b/src/components/repo/authv2.ts index 57709fb..0fc85b1 100644 --- a/src/components/repo/authv2.ts +++ b/src/components/repo/authv2.ts @@ -18,7 +18,6 @@ */ import {readable, writable} from "svelte/store"; -import {tokenStore} from "@repo/repo.ts"; import dayjs, {type Dayjs} from "dayjs"; import {type AuthToken, AuthTokenSchema} from "@type/auth.ts"; @@ -152,7 +151,11 @@ export class AuthV2Repo { return this.requestWithToken(this.accessToken ?? "", url, params); } - private async requestWithToken(token: string, url: string, params: RequestInit = {}) { + private async requestWithToken(token: string, url: string, params: RequestInit = {}, retryCount: number = 0): Promise { + if (retryCount >= 3) { + throw new Error("Too many retries"); + } + return fetch(`${import.meta.env.PUBLIC_API_SERVER}${url}`, {...params, headers: { ...(token !== "" ? {"Authorization": "Bearer " + (token)} : {}), @@ -161,7 +164,9 @@ export class AuthV2Repo { }) .then(value => { if (value.status === 401) { - tokenStore.set(""); + this.refresh(); + + return this.requestWithToken(token, url, params, retryCount + 1); } return value; });