From 7d1d3a80cf51957608920659fa991f630e2c1470 Mon Sep 17 00:00:00 2001 From: Hetmann Wilhelm Date: Wed, 18 Jun 2025 13:08:23 +0300 Subject: [PATCH] fix: implemented cancel scenario in oauthSignIn for signInWithRedirect --- .../cognito/signInWithRedirect.test.ts | 24 +++++++++++++++++++ .../cognito/apis/signInWithRedirect.ts | 3 +++ 2 files changed, 27 insertions(+) diff --git a/packages/auth/__tests__/providers/cognito/signInWithRedirect.test.ts b/packages/auth/__tests__/providers/cognito/signInWithRedirect.test.ts index 0714c091278..4a0d60490f9 100644 --- a/packages/auth/__tests__/providers/cognito/signInWithRedirect.test.ts +++ b/packages/auth/__tests__/providers/cognito/signInWithRedirect.test.ts @@ -296,6 +296,30 @@ describe('signInWithRedirect', () => { expect(mockHandleFailure).toHaveBeenCalledWith(expectedError); }); + it('invokes `handleFailure` with the error thrown from `completeOAuthFlow`', async () => { + const expectedError = new Error('OAuth flow was cancelled.'); + const mockOpenAuthSessionResult = { + type: 'canceled', + url: 'https://url.com', + }; + mockOpenAuthSession.mockResolvedValueOnce(mockOpenAuthSessionResult); + mockCompleteOAuthFlow.mockRejectedValueOnce(expectedError); + + await expect( + signInWithRedirect({ + provider: 'Google', + options: { preferPrivateSession: true }, + }), + ).rejects.toThrow(expectedError); + + expect(mockCompleteOAuthFlow).toHaveBeenCalledWith( + expect.objectContaining({ + currentUrl: mockOpenAuthSessionResult.url, + }), + ); + expect(mockHandleFailure).toHaveBeenCalledWith(expectedError); + }); + it('should not set the Oauth flag on non-browser environments', async () => { const mockOpenAuthSessionResult = { type: 'success', diff --git a/packages/auth/src/providers/cognito/apis/signInWithRedirect.ts b/packages/auth/src/providers/cognito/apis/signInWithRedirect.ts index bac92589dc8..aeba2254ac4 100644 --- a/packages/auth/src/providers/cognito/apis/signInWithRedirect.ts +++ b/packages/auth/src/providers/cognito/apis/signInWithRedirect.ts @@ -137,6 +137,9 @@ const oauthSignIn = async ({ if (type === 'error') { throw createOAuthError(String(error)); } + if (type === 'canceled') { + throw createOAuthError(String(type)); + } if (type === 'success' && url) { await completeOAuthFlow({ currentUrl: url,