From 44fdfe80476a59510514b842da3e93a6472bf506 Mon Sep 17 00:00:00 2001 From: israx <70438514+israx@users.noreply.github.com> Date: Thu, 4 Jan 2024 11:11:03 -0500 Subject: [PATCH] fix(auth): replace window history with current browser session's state (#12781) * chore: replace history with current session's state * chore: update window.history.state * chore: fix unit test --- .../cognito/utils/oauth/completeOAuthFlow.test.ts | 7 ++++--- .../src/providers/cognito/utils/oauth/completeOAuthFlow.ts | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/auth/__tests__/providers/cognito/utils/oauth/completeOAuthFlow.test.ts b/packages/auth/__tests__/providers/cognito/utils/oauth/completeOAuthFlow.test.ts index 0fa26074629..bbfe51e28ea 100644 --- a/packages/auth/__tests__/providers/cognito/utils/oauth/completeOAuthFlow.test.ts +++ b/packages/auth/__tests__/providers/cognito/utils/oauth/completeOAuthFlow.test.ts @@ -66,7 +66,7 @@ describe('completeOAuthFlow', () => { let windowSpy = jest.spyOn(window, 'window', 'get'); const mockFetch = jest.fn(); const mockReplaceState = jest.fn(); - + beforeAll(() => { (global as any).fetch = mockFetch; windowSpy.mockImplementation( @@ -74,6 +74,7 @@ describe('completeOAuthFlow', () => { ({ history: { replaceState: mockReplaceState, + state:'http://localhost:3000/?code=aaaa-111-222&state=aaaaa' }, }) as any ); @@ -192,7 +193,7 @@ describe('completeOAuthFlow', () => { ExpiresIn: expectedTokens.expires_in, }); expect(mockReplaceState).toHaveBeenCalledWith( - {}, + 'http://localhost:3000/?code=aaaa-111-222&state=aaaaa', '', testInput.redirectUri ); @@ -303,7 +304,7 @@ describe('completeOAuthFlow', () => { expect(mockHubDispatch).toHaveBeenCalledTimes(2); expect(mockReplaceState).toHaveBeenCalledWith( - {}, + 'http://localhost:3000/?code=aaaa-111-222&state=aaaaa', '', testInput.redirectUri ); diff --git a/packages/auth/src/providers/cognito/utils/oauth/completeOAuthFlow.ts b/packages/auth/src/providers/cognito/utils/oauth/completeOAuthFlow.ts index 36adb7dd778..b15d8ea63cd 100644 --- a/packages/auth/src/providers/cognito/utils/oauth/completeOAuthFlow.ts +++ b/packages/auth/src/providers/cognito/utils/oauth/completeOAuthFlow.ts @@ -269,6 +269,6 @@ const getCustomState = (state: string): string => { const clearHistory = (redirectUri: string) => { if (typeof window !== 'undefined' && typeof window.history !== 'undefined') { - window.history.replaceState({}, '', redirectUri); + window.history.replaceState(window.history.state, '', redirectUri); } };