From de88bb01c44a6f2039541c9b5906fdf33569e265 Mon Sep 17 00:00:00 2001 From: Mikhala Date: Thu, 19 Dec 2024 08:56:56 +0800 Subject: [PATCH 1/4] ID-3129 Prevent `Unknown or invalid refresh token` requests from being triggered multiple times --- packages/passport/sdk/src/authManager.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/passport/sdk/src/authManager.ts b/packages/passport/sdk/src/authManager.ts index f1fafab3ef..99df46cff3 100644 --- a/packages/passport/sdk/src/authManager.ts +++ b/packages/passport/sdk/src/authManager.ts @@ -481,18 +481,24 @@ export default class AuthManager { } catch (err) { let passportErrorType = PassportErrorType.AUTHENTICATION_ERROR; let errorMessage = 'Failed to refresh token'; + let removeUser = true; if (err instanceof ErrorTimeout) { passportErrorType = PassportErrorType.SILENT_LOGIN_ERROR; + removeUser = false; } else if (err instanceof ErrorResponse) { passportErrorType = PassportErrorType.NOT_LOGGED_IN_ERROR; - errorMessage = `${err.message}: ${err.error_description}`; + errorMessage = `${errorMessage}: ${err.message || err.error_description}`; } else if (err instanceof Error) { - errorMessage = err.message; + errorMessage = `${errorMessage}: ${err.message}`; } else if (typeof err === 'string') { errorMessage = `${errorMessage}: ${err}`; } + if (removeUser) { + await this.userManager.removeUser(); + } + reject(new PassportError(errorMessage, passportErrorType)); } finally { this.refreshingPromise = null; // Reset the promise after completion From 514f3df43ccc73b63885e76fc4662f910d93880e Mon Sep 17 00:00:00 2001 From: Mikhala Date: Thu, 19 Dec 2024 09:35:28 +0800 Subject: [PATCH 2/4] catch if removeUser errors --- packages/passport/sdk/src/authManager.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/passport/sdk/src/authManager.ts b/packages/passport/sdk/src/authManager.ts index 99df46cff3..54bea4f844 100644 --- a/packages/passport/sdk/src/authManager.ts +++ b/packages/passport/sdk/src/authManager.ts @@ -496,7 +496,13 @@ export default class AuthManager { } if (removeUser) { - await this.userManager.removeUser(); + try { + await this.userManager.removeUser(); + } catch (removeUserError) { + if (removeUserError instanceof Error) { + errorMessage = `${errorMessage}: Failed to remove user: ${removeUserError.message}`; + } + } } reject(new PassportError(errorMessage, passportErrorType)); From ce9f83533583fd4ee2673d3e86b49a83cfabfec1 Mon Sep 17 00:00:00 2001 From: Mikhala Date: Thu, 19 Dec 2024 09:41:39 +0800 Subject: [PATCH 3/4] update msg --- packages/passport/sdk/src/authManager.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/passport/sdk/src/authManager.ts b/packages/passport/sdk/src/authManager.ts index 54bea4f844..fc3edc4aba 100644 --- a/packages/passport/sdk/src/authManager.ts +++ b/packages/passport/sdk/src/authManager.ts @@ -485,6 +485,7 @@ export default class AuthManager { if (err instanceof ErrorTimeout) { passportErrorType = PassportErrorType.SILENT_LOGIN_ERROR; + errorMessage = `${errorMessage}: ${err.message}`; removeUser = false; } else if (err instanceof ErrorResponse) { passportErrorType = PassportErrorType.NOT_LOGGED_IN_ERROR; From a752c4462e68ebf5f67beb0363df34c6d7e20ed1 Mon Sep 17 00:00:00 2001 From: Mikhala Date: Thu, 19 Dec 2024 09:54:10 +0800 Subject: [PATCH 4/4] update test --- packages/passport/sdk/src/authManager.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/passport/sdk/src/authManager.test.ts b/packages/passport/sdk/src/authManager.test.ts index 54f2c40f94..3bd7bc0ddd 100644 --- a/packages/passport/sdk/src/authManager.test.ts +++ b/packages/passport/sdk/src/authManager.test.ts @@ -460,7 +460,7 @@ describe('AuthManager', () => { await expect(() => authManager.getUser()).rejects.toThrow( new PassportError( - 'Failed to refresh token: oops', + 'Failed to refresh token: oops: Failed to remove user: this.userManager.removeUser is not a function', PassportErrorType.AUTHENTICATION_ERROR, ), );