Skip to content

Commit

Permalink
fix(auth): replace window history with current browser session's state (
Browse files Browse the repository at this point in the history
aws-amplify#12781)

* chore: replace history with current session's state

* chore: update window.history.state

* chore: fix unit test
  • Loading branch information
israx committed Jan 4, 2024
1 parent 1bcbae4 commit 44fdfe8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ 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(
() =>
({
history: {
replaceState: mockReplaceState,
state:'http://localhost:3000/?code=aaaa-111-222&state=aaaaa'
},
}) as any
);
Expand Down Expand Up @@ -192,7 +193,7 @@ describe('completeOAuthFlow', () => {
ExpiresIn: expectedTokens.expires_in,
});
expect(mockReplaceState).toHaveBeenCalledWith(
{},
'http://localhost:3000/?code=aaaa-111-222&state=aaaaa',
'',
testInput.redirectUri
);
Expand Down Expand Up @@ -303,7 +304,7 @@ describe('completeOAuthFlow', () => {

expect(mockHubDispatch).toHaveBeenCalledTimes(2);
expect(mockReplaceState).toHaveBeenCalledWith(
{},
'http://localhost:3000/?code=aaaa-111-222&state=aaaaa',
'',
testInput.redirectUri
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

0 comments on commit 44fdfe8

Please sign in to comment.