diff --git a/src/auth/google/google.service.ts b/src/auth/google/google.service.ts index 2ee8e916..b80931d2 100644 --- a/src/auth/google/google.service.ts +++ b/src/auth/google/google.service.ts @@ -8,6 +8,7 @@ import { CreateUserBindingDto } from 'omniboxd/user/dto/create-user-binding.dto' import { Injectable, Logger, HttpStatus } from '@nestjs/common'; import { AppException } from 'omniboxd/common/exceptions/app.exception'; import { I18nService } from 'nestjs-i18n'; +import { fetchWithRetry } from 'omniboxd/utils/fetch-with-retry'; interface GoogleTokenResponse { access_token: string; @@ -103,19 +104,22 @@ export class GoogleService { ); } - const tokenResponse = await fetch(`${this.googleOAuthAPIBaseUrl}/token`, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', + const tokenResponse = await fetchWithRetry( + `${this.googleOAuthAPIBaseUrl}/token`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: new URLSearchParams({ + client_id: this.clientId, + client_secret: this.clientSecret, + code: code, + grant_type: 'authorization_code', + redirect_uri: this.redirectUri, + }), }, - body: new URLSearchParams({ - client_id: this.clientId, - client_secret: this.clientSecret, - code: code, - grant_type: 'authorization_code', - redirect_uri: this.redirectUri, - }), - }); + ); if (!tokenResponse.ok) { const providerName = this.i18n.t('auth.providers.google'); @@ -139,7 +143,7 @@ export class GoogleService { ); } - const userInfoResponse = await fetch( + const userInfoResponse = await fetchWithRetry( `${this.googleAPIBaseUrl}/oauth2/v3/userinfo`, { headers: { Authorization: `Bearer ${tokenData.access_token}` }, diff --git a/src/utils/fetch-with-retry.ts b/src/utils/fetch-with-retry.ts new file mode 100644 index 00000000..2f95cca3 --- /dev/null +++ b/src/utils/fetch-with-retry.ts @@ -0,0 +1,14 @@ +export async function fetchWithRetry( + url: string, + options?: RequestInit, + maxRetries = 2, +): Promise { + for (let i = 0; i < maxRetries; i++) { + try { + return await fetch(url, options); + } catch { + // ignore the error + } + } + return await fetch(url, options); +}