Skip to content

Commit f8f2904

Browse files
hetmannHetmann Wilhelmsoberm
authored
fix: implemented cancel scenario in oauthSignIn for signInWithRedirect (#14434)
* fix: implemented cancel scenario in oauthSignIn for signInWithRedirect --------- Co-authored-by: Hetmann Wilhelm <[email protected]> Co-authored-by: Michael Sober <[email protected]>
1 parent 70182e6 commit f8f2904

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

packages/auth/__tests__/providers/cognito/signInWithRedirect.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ describe('signInWithRedirect', () => {
137137
mockToCodeChallenge.mockClear();
138138
mockHandleFailure.mockClear();
139139
mockCompleteOAuthFlow.mockClear();
140+
mockCreateOAuthError.mockClear();
140141

141142
(oAuthStore.setAuthConfig as jest.Mock).mockClear();
142143
(oAuthStore.storeOAuthInFlight as jest.Mock).mockClear();
@@ -363,6 +364,28 @@ describe('signInWithRedirect', () => {
363364
expect(mockHandleFailure).toHaveBeenCalledWith(expectedError);
364365
});
365366

367+
it('invokes `handleFailure` with the error created by `createOAuthError` when `openAuthSession` is canceled', async () => {
368+
const expectedError = new Error('OAuth flow was cancelled.');
369+
const mockOpenAuthSessionResult = {
370+
type: 'canceled',
371+
};
372+
373+
mockOpenAuthSession.mockResolvedValueOnce(mockOpenAuthSessionResult);
374+
mockCreateOAuthError.mockReturnValueOnce(expectedError);
375+
376+
await expect(
377+
signInWithRedirect({
378+
provider: 'Google',
379+
options: { preferPrivateSession: true },
380+
}),
381+
).rejects.toThrow(expectedError);
382+
383+
expect(mockCreateOAuthError).toHaveBeenCalledWith(
384+
mockOpenAuthSessionResult.type,
385+
);
386+
expect(mockHandleFailure).toHaveBeenCalledWith(expectedError);
387+
});
388+
366389
it('should not set the Oauth flag on non-browser environments', async () => {
367390
const mockOpenAuthSessionResult = {
368391
type: 'success',

packages/auth/src/providers/cognito/apis/signInWithRedirect.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ const oauthSignIn = async ({
149149
if (type === 'error') {
150150
throw createOAuthError(String(error));
151151
}
152+
if (type === 'canceled') {
153+
throw createOAuthError(String(type));
154+
}
152155
if (type === 'success' && url) {
153156
await completeOAuthFlow({
154157
currentUrl: url,

0 commit comments

Comments
 (0)