From add762e31b37c4b6b7f8bac98fa674ff01015be0 Mon Sep 17 00:00:00 2001 From: Stojan Dimitrovski Date: Mon, 26 Jun 2023 13:05:57 +0200 Subject: [PATCH] feat: `_recoverAndRefresh` does not remove session on retryable error (#710) If the `_callRefreshToken` method failed with a network error, the session would have been removed, which should generally not be the case as the device is offline. --- src/GoTrueClient.ts | 8 ++++++-- src/lib/errors.ts | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index 03a5dca6d..0faa2f04f 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -11,6 +11,7 @@ import { AuthUnknownError, isAuthApiError, isAuthError, + isAuthRetryableFetchError, } from './lib/errors' import { Fetch, _request, _sessionResponse, _userResponse, _ssoResponse } from './lib/fetch' import { @@ -1274,8 +1275,11 @@ export default class GoTrueClient { const { error } = await this._callRefreshToken(currentSession.refresh_token) if (error) { - console.log(error.message) - await this._removeSession() + console.error(error) + + if (!isAuthRetryableFetchError(error)) { + await this._removeSession() + } } } } else { diff --git a/src/lib/errors.ts b/src/lib/errors.ts index 40528053b..7fe8561d2 100644 --- a/src/lib/errors.ts +++ b/src/lib/errors.ts @@ -120,3 +120,7 @@ export class AuthRetryableFetchError extends CustomAuthError { super(message, 'AuthRetryableFetchError', status) } } + +export function isAuthRetryableFetchError(error: unknown): error is AuthRetryableFetchError { + return isAuthError(error) && error.name === 'AuthRetryableFetchError' +}