Skip to content

Commit

Permalink
refactor: Enhance token refresh mechanism with retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bigint committed Mar 6, 2025
1 parent 63007a0 commit 18fa7c0
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions apps/web/src/helpers/authLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ const REFRESH_AUTHENTICATION_MUTATION = `

let refreshPromise: Promise<string> | null = null;

const executeTokenRefresh = async (refreshToken: string): Promise<string> => {
const MAX_RETRIES = 5;

const executeTokenRefresh = async (
refreshToken: string,
attempt = 0
): Promise<string> => {
try {
const response = await axios.post(
LENS_API_URL,
Expand All @@ -37,9 +42,14 @@ const executeTokenRefresh = async (refreshToken: string): Promise<string> => {
const {
accessToken,
refreshToken: newRefreshToken,
idToken
idToken,
__typename
} = response?.data?.data?.refresh ?? {};

if (__typename !== "AuthenticationTokens" && attempt < MAX_RETRIES) {
return executeTokenRefresh(refreshToken, attempt + 1);
}

if (!accessToken || !newRefreshToken) {
signOut();
throw new Error("Invalid refresh response");
Expand All @@ -59,6 +69,7 @@ const refreshTokens = (refreshToken: string): Promise<string> => {
if (!refreshPromise) {
refreshPromise = executeTokenRefresh(refreshToken);
}

return refreshPromise;
};

Expand Down

0 comments on commit 18fa7c0

Please sign in to comment.