diff --git a/.changeset/sixty-buckets-repeat.md b/.changeset/sixty-buckets-repeat.md new file mode 100644 index 00000000000..40591209839 --- /dev/null +++ b/.changeset/sixty-buckets-repeat.md @@ -0,0 +1,5 @@ +--- +'@firebase/auth': patch +--- + +Modify \_fail to use AuthErrorCode.NETWORK_REQUEST_FAILED diff --git a/packages/auth/src/api/index.test.ts b/packages/auth/src/api/index.test.ts index 579f6c5e8ad..af3b61610d2 100644 --- a/packages/auth/src/api/index.test.ts +++ b/packages/auth/src/api/index.test.ts @@ -308,26 +308,6 @@ describe('api/_performApiRequest', () => { }); }); - context('with non-Firebase Errors', () => { - afterEach(mockFetch.tearDown); - - it('should handle non-FirebaseErrors', async () => { - mockFetch.setUpWithOverride(() => { - return new Promise((_, reject) => reject(new Error('error'))); - }); - const promise = _performApiRequest( - auth, - HttpMethod.POST, - Endpoint.SIGN_UP, - request - ); - await expect(promise).to.be.rejectedWith( - FirebaseError, - 'auth/internal-error' - ); - }); - }); - context('with network issues', () => { afterEach(mockFetch.tearDown); @@ -365,6 +345,26 @@ describe('api/_performApiRequest', () => { expect(clock.clearTimeout).to.have.been.called; clock.restore(); }); + + it('should handle network failure', async () => { + mockFetch.setUpWithOverride(() => { + return new Promise((_, reject) => + reject(new Error('network error')) + ); + }); + const promise = _performApiRequest( + auth, + HttpMethod.POST, + Endpoint.SIGN_UP, + request + ); + await expect(promise) + .to.be.rejectedWith(FirebaseError, 'auth/network-request-failed') + .eventually.with.nested.property( + 'customData.message', + 'Error: network error' + ); + }); }); context('edgcase error mapping', () => { diff --git a/packages/auth/src/api/index.ts b/packages/auth/src/api/index.ts index 0194c43d9a5..6c9d775d243 100644 --- a/packages/auth/src/api/index.ts +++ b/packages/auth/src/api/index.ts @@ -181,7 +181,10 @@ export async function _performFetchWithErrorHandling( if (e instanceof FirebaseError) { throw e; } - _fail(auth, AuthErrorCode.INTERNAL_ERROR, { 'message': String(e) }); + // Changing this to a different error code will log user out when there is a network error + // because we treat any error other than NETWORK_REQUEST_FAILED as token is invalid. + // https://github.com/firebase/firebase-js-sdk/blob/4fbc73610d70be4e0852e7de63a39cb7897e8546/packages/auth/src/core/auth/auth_impl.ts#L309-L316 + _fail(auth, AuthErrorCode.NETWORK_REQUEST_FAILED, { 'message': String(e) }); } }