Update WorkOsAuthProvider.ts
This commit is contained in:
parent
ce68da7f36
commit
b8a15d66af
1 changed files with 46 additions and 1 deletions
|
|
@ -190,7 +190,9 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
|
|||
const finalSessions = [];
|
||||
for (const session of sessions) {
|
||||
try {
|
||||
const newSession = await this._refreshSession(session.refreshToken);
|
||||
const newSession = await this._refreshSessionWithRetry(
|
||||
session.refreshToken,
|
||||
);
|
||||
finalSessions.push({
|
||||
...session,
|
||||
accessToken: newSession.accessToken,
|
||||
|
|
@ -216,6 +218,49 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
|
|||
});
|
||||
}
|
||||
|
||||
private async _refreshSessionWithRetry(
|
||||
refreshToken: string,
|
||||
attempt = 1,
|
||||
maxAttempts = 3,
|
||||
baseDelay = 1000,
|
||||
): Promise<{
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
expiresInMs: number;
|
||||
}> {
|
||||
try {
|
||||
return await this._refreshSession(refreshToken);
|
||||
} catch (error: any) {
|
||||
// Don't retry for auth errors (401 Unauthorized) or server errors that indicate invalid token
|
||||
if (
|
||||
error.message?.includes("401") ||
|
||||
error.message?.includes("Invalid refresh token") ||
|
||||
attempt >= maxAttempts
|
||||
) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
// For network errors or transient server issues, retry with backoff
|
||||
// Calculate exponential backoff delay with jitter
|
||||
const delay =
|
||||
baseDelay * Math.pow(2, attempt - 1) * (0.5 + Math.random() * 0.5);
|
||||
|
||||
// Schedule retry after delay
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
this._refreshSessionWithRetry(
|
||||
refreshToken,
|
||||
attempt + 1,
|
||||
maxAttempts,
|
||||
baseDelay,
|
||||
)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
}, delay);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async _refreshSession(refreshToken: string): Promise<{
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue