Skip to content

Commit e924f12

Browse files
fix(auth): clear browser tab location before sending final success hub events during OAuth signIn (#14188)
fix: clear history before and updates tests Co-authored-by: Hui Zhao <[email protected]>
1 parent 9145612 commit e924f12

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

packages/auth/__tests__/providers/cognito/utils/oauth/completeOAuthFlow.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ describe('completeOAuthFlow', () => {
151151
token_type: 'token_type',
152152
expires_in: 'expires_in',
153153
};
154+
const executionOrder: string[] = [];
155+
154156
mockValidateState.mockReturnValueOnce('myState-valid_state');
155157
(oAuthStore.loadPKCE as jest.Mock).mockResolvedValueOnce('pkce23234a');
156158
const mockJsonMethod = jest.fn(() => Promise.resolve(expectedTokens));
@@ -162,6 +164,12 @@ describe('completeOAuthFlow', () => {
162164
mockFetch.mockResolvedValueOnce({
163165
json: mockJsonMethod,
164166
});
167+
mockReplaceState.mockImplementation((..._args) =>
168+
executionOrder.push('replaceState'),
169+
);
170+
mockHubDispatch.mockImplementation(() =>
171+
executionOrder.push('hubDispatch'),
172+
);
165173

166174
await completeOAuthFlow(testInput);
167175

@@ -180,17 +188,27 @@ describe('completeOAuthFlow', () => {
180188
TokenType: expectedTokens.token_type,
181189
ExpiresIn: expectedTokens.expires_in,
182190
});
191+
192+
expect(oAuthStore.clearOAuthData).toHaveBeenCalledTimes(1);
193+
expect(oAuthStore.storeOAuthSignIn).toHaveBeenCalledWith(true, undefined);
194+
195+
expect(mockResolveAndClearInflightPromises).toHaveBeenCalledTimes(1);
196+
183197
expect(mockReplaceState).toHaveBeenCalledWith(
184198
'http://localhost:3000/?code=aaaa-111-222&state=aaaaa',
185199
'',
186200
testInput.redirectUri,
187201
);
188202

189-
expect(oAuthStore.clearOAuthData).toHaveBeenCalledTimes(1);
190-
expect(oAuthStore.storeOAuthSignIn).toHaveBeenCalledWith(true, undefined);
191-
192203
expect(mockHubDispatch).toHaveBeenCalledTimes(3);
193-
expect(mockResolveAndClearInflightPromises).toHaveBeenCalledTimes(1);
204+
205+
// Verify we replace browser tab location before dispatching hub events
206+
expect(executionOrder).toEqual([
207+
'replaceState',
208+
'hubDispatch',
209+
'hubDispatch',
210+
'hubDispatch',
211+
]);
194212
});
195213

196214
it('throws when `fetch` call resolves error', async () => {

packages/auth/src/providers/cognito/utils/oauth/completeOAuthFlow.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ const completeFlow = async ({
239239
// `fetchAuthSession` can be resolved
240240
resolveAndClearInflightPromises();
241241

242+
// clear history before sending out final Hub events
243+
clearHistory(redirectUri);
244+
242245
if (isCustomState(state)) {
243246
Hub.dispatch(
244247
'auth',
@@ -252,7 +255,6 @@ const completeFlow = async ({
252255
}
253256
Hub.dispatch('auth', { event: 'signInWithRedirect' }, 'Auth', AMPLIFY_SYMBOL);
254257
await dispatchSignedInHubEvent();
255-
clearHistory(redirectUri);
256258
};
257259

258260
const isCustomState = (state: string): boolean => {

0 commit comments

Comments
 (0)