Skip to content

Commit

Permalink
apply code rabbit suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Büßemeyer authored and Michael Büßemeyer committed Oct 24, 2024
1 parent 69ec689 commit 1660fa7
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions frontend/javascripts/admin/api/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { location } from "libs/window";
import Request from "libs/request";
import * as Utils from "libs/utils";

const MAX_TOKEN_RETRY_ATTEMPTS = 3;

let tokenPromise: Promise<string>;

let tokenRequestPromise: Promise<string> | null;
Expand Down Expand Up @@ -50,11 +52,13 @@ export async function doWithToken<T>(

return tokenPromise.then(fn).catch(async (error) => {
if (error.status === 403) {
console.warn("Token expired. Requesting new token...");
console.warn(
`Token expired (attempt ${tries}/${MAX_TOKEN_RETRY_ATTEMPTS}). Requesting new token...`,
);
tokenPromise = requestUserToken();

// If three new tokens did not fix the 403, abort, otherwise we'll get into an endless loop here
if (tries < 3) {
if (tries < MAX_TOKEN_RETRY_ATTEMPTS) {
// If using the url sharing token failed, we try the user specific token instead.
const result = await doWithToken(fn, tries + 1, false);
// Upon successful retry with own token, discard the url token.
Expand Down

0 comments on commit 1660fa7

Please sign in to comment.