From 1e8b74f795c577e33da376e6cf7f82283a35e9da Mon Sep 17 00:00:00 2001 From: Kristian Balaj Date: Sun, 30 Aug 2020 13:07:52 +0200 Subject: [PATCH 1/8] feat(firebase_auth): added enum for auth exception codes --- .../lib/src/auth_exception_status_code.dart | 26 +++++++++++++++++++ .../lib/src/firebase_auth_exception.dart | 23 ++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart new file mode 100644 index 000000000000..012382504028 --- /dev/null +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart @@ -0,0 +1,26 @@ +/// The codes of the auth exceptions. +enum AuthExceptionStatusCode { + /// Used if the email address is not valid. + invalidEmail, + + /// Used if the user corresponding to the given email has been disabled. + userDisabled, + + /// Used if there is no user corresponding to the given email. + userNotFound, + + /// Used if the password is invalid for the given email, or the account corresponding to the email does not have a password set. + wrongPassword, + + /// Used if the Firebase Authentication quota is reached. + tooManyRequests, + + /// Used if specific auth provider is not enabled. + operationNotAllowed, + + /// Used if the email exists for multiple Firebase user's providers. + accountExistsWithDifferentCredential, + + /// Used if the status is undefined. + undefined +} diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart index af7151505ece..561d36476922 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:firebase_auth_platform_interface/src/auth_exception_status_code.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:meta/meta.dart'; import 'auth_credential.dart'; @@ -37,4 +38,26 @@ class FirebaseAuthException extends FirebaseException implements Exception { /// The tenant ID being used for sign-in/linking. final String tenantId; + + /// The exception code parsed into the [AuthExceptionStatusCode] enum. + AuthExceptionStatusCode get statusCode { + switch (code) { + case 'invalid-email': + return AuthExceptionStatusCode.invalidEmail; + case 'user-disabled': + return AuthExceptionStatusCode.userDisabled; + case 'user-not-found': + return AuthExceptionStatusCode.userNotFound; + case 'wrong-password': + return AuthExceptionStatusCode.wrongPassword; + case 'too-many-requests': + return AuthExceptionStatusCode.tooManyRequests; + case 'operation-not-allowed': + return AuthExceptionStatusCode.operationNotAllowed; + case 'account-exists-with-different-credential': + return AuthExceptionStatusCode.accountExistsWithDifferentCredential; + default: + return AuthExceptionStatusCode.undefined; + } + } } From bb8b2e60b0583d1ff6a040c833044afbbcd3a1e1 Mon Sep 17 00:00:00 2001 From: Kristian Balaj Date: Sun, 30 Aug 2020 13:15:16 +0200 Subject: [PATCH 2/8] feat(firebase_auth): added networkRequestFailed auth exception status code --- .../lib/src/auth_exception_status_code.dart | 3 +++ .../lib/src/firebase_auth_exception.dart | 2 ++ 2 files changed, 5 insertions(+) diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart index 012382504028..c2db78a5304a 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart @@ -21,6 +21,9 @@ enum AuthExceptionStatusCode { /// Used if the email exists for multiple Firebase user's providers. accountExistsWithDifferentCredential, + /// Used if the request failed due to network issues. + networkRequestFailed, + /// Used if the status is undefined. undefined } diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart index 561d36476922..567a30d123fb 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart @@ -56,6 +56,8 @@ class FirebaseAuthException extends FirebaseException implements Exception { return AuthExceptionStatusCode.operationNotAllowed; case 'account-exists-with-different-credential': return AuthExceptionStatusCode.accountExistsWithDifferentCredential; + case 'network-request-failed': + return AuthExceptionStatusCode.networkRequestFailed; default: return AuthExceptionStatusCode.undefined; } From 68a69f405230e5765aef920387635ba0c3bad8da Mon Sep 17 00:00:00 2001 From: Kristian Balaj Date: Mon, 31 Aug 2020 19:59:03 +0200 Subject: [PATCH 3/8] feat(firebase_auth): added missing auth exception status codes --- .../lib/src/auth_exception_status_code.dart | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart index c2db78a5304a..b302281b33bb 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart @@ -24,6 +24,27 @@ enum AuthExceptionStatusCode { /// Used if the request failed due to network issues. networkRequestFailed, - /// Used if the status is undefined. - undefined + /// Used if a user being created already exists. + emailAlreadyInUse, + + /// Used if the request to create a user has a weak password. + weakPassword, + + /// Used if the phone verification fails with an invalid phone number. + invalidPhoneNumber, + + /// Used if the verification ID used to create the phone auth credential is invalid. + invalidVerificationId, + + /// Used if the supplied credentials do not correspond to the previously signed in user. + userMismatch, + + /// Used if the user was not linked to an account with the given provider. + noSuchProvider, + + /// Used if there is no user currently signed in. + noCurrentUser, + + /// Used if the status is unknown. + unknown } From f3ac21ef5dc73780cee6457b7ddfdc2b7a99fd43 Mon Sep 17 00:00:00 2001 From: Kristian Balaj Date: Mon, 31 Aug 2020 20:01:26 +0200 Subject: [PATCH 4/8] feat(firebase_auth): deprecated the code in FirebaseAuthException & implemented the new status codes. Exported AuthExceptionStatusCode enum. --- .../firebase_auth/lib/firebase_auth.dart | 3 ++- .../lib/firebase_auth_platform_interface.dart | 1 + .../lib/src/firebase_auth_exception.dart | 24 ++++++++++++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/lib/firebase_auth.dart b/packages/firebase_auth/firebase_auth/lib/firebase_auth.dart index b781239ab0d2..35bb765c8797 100755 --- a/packages/firebase_auth/firebase_auth/lib/firebase_auth.dart +++ b/packages/firebase_auth/firebase_auth/lib/firebase_auth.dart @@ -47,7 +47,8 @@ export 'package:firebase_auth_platform_interface/firebase_auth_platform_interfac RecaptchaVerifierOnExpired, RecaptchaVerifierOnError, RecaptchaVerifierSize, - RecaptchaVerifierTheme; + RecaptchaVerifierTheme, + AuthExceptionStatusCode; export 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart' show FirebaseException; diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/firebase_auth_platform_interface.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/firebase_auth_platform_interface.dart index 3d9dbbdebdde..c1b14d036da3 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/firebase_auth_platform_interface.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/firebase_auth_platform_interface.dart @@ -11,6 +11,7 @@ export 'src/platform_interface/platform_interface_confirmation_result.dart'; export 'src/platform_interface/platform_interface_recaptcha_verifier_factory.dart'; export 'src/firebase_auth_exception.dart'; +export 'src/auth_exception_status_code.dart'; export 'src/auth_credential.dart'; export 'src/action_code_info.dart'; export 'src/action_code_settings.dart'; diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart index 567a30d123fb..6c813fd13f97 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart @@ -14,15 +14,15 @@ class FirebaseAuthException extends FirebaseException implements Exception { @protected FirebaseAuthException( {@required this.message, - this.code, + String code, this.email, this.credential, this.phoneNumber, this.tenantId}) : super(plugin: 'firebase_auth', message: message, code: code); - /// Unique error code - final String code; + @Deprecated('Deprecated in favor of `.statusCode`.') + String get code => super.code; /// Complete error message. final String message; @@ -39,9 +39,9 @@ class FirebaseAuthException extends FirebaseException implements Exception { /// The tenant ID being used for sign-in/linking. final String tenantId; - /// The exception code parsed into the [AuthExceptionStatusCode] enum. + /// The error code. AuthExceptionStatusCode get statusCode { - switch (code) { + switch (super.code) { case 'invalid-email': return AuthExceptionStatusCode.invalidEmail; case 'user-disabled': @@ -58,8 +58,20 @@ class FirebaseAuthException extends FirebaseException implements Exception { return AuthExceptionStatusCode.accountExistsWithDifferentCredential; case 'network-request-failed': return AuthExceptionStatusCode.networkRequestFailed; + case 'email-already-in-use': + return AuthExceptionStatusCode.emailAlreadyInUse; + case 'weak-password': + return AuthExceptionStatusCode.weakPassword; + case 'invalid-phone-number': + return AuthExceptionStatusCode.invalidPhoneNumber; + case 'invalid-verification-id': + return AuthExceptionStatusCode.invalidVerificationId; + case 'user-mismatch': + return AuthExceptionStatusCode.userMismatch; + case 'no-such-provider': + return AuthExceptionStatusCode.noSuchProvider; default: - return AuthExceptionStatusCode.undefined; + return AuthExceptionStatusCode.unknown; } } } From f45c7ce46190279875a1df09127b8d73c9da0460 Mon Sep 17 00:00:00 2001 From: Kristian Balaj Date: Mon, 31 Aug 2020 20:02:31 +0200 Subject: [PATCH 5/8] test(firebase_auth): modified tests to use the new AuthExceptionStatusCodes. --- .../example/lib/signin_page.dart | 2 +- .../example/test_driver/instance_e2e.dart | 13 +++++----- .../example/test_driver/test_utils.dart | 2 +- .../example/test_driver/user_e2e.dart | 25 ++++++++++--------- .../utils_tests/exception_test.dart | 16 ++++++++---- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/example/lib/signin_page.dart b/packages/firebase_auth/firebase_auth/example/lib/signin_page.dart index 444d101e19a0..dd1033e2ea67 100644 --- a/packages/firebase_auth/firebase_auth/example/lib/signin_page.dart +++ b/packages/firebase_auth/firebase_auth/example/lib/signin_page.dart @@ -435,7 +435,7 @@ class _PhoneSignInSectionState extends State<_PhoneSignInSection> { (FirebaseAuthException authException) { setState(() { _message = - 'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}'; + 'Phone number verification failed. Code: ${authException.statusCode}. Message: ${authException.message}'; }); }; diff --git a/packages/firebase_auth/firebase_auth/example/test_driver/instance_e2e.dart b/packages/firebase_auth/firebase_auth/example/test_driver/instance_e2e.dart index 52e6728d61cc..c9fbb6ca0855 100644 --- a/packages/firebase_auth/firebase_auth/example/test_driver/instance_e2e.dart +++ b/packages/firebase_auth/firebase_auth/example/test_driver/instance_e2e.dart @@ -262,7 +262,7 @@ void runInstanceTests() { email: regularTestEmail, password: '123456'); fail("Should have thrown FirebaseAuthException"); } on FirebaseAuthException catch (e) { - expect(e.code, equals('email-already-in-use')); + expect(e.statusCode, AuthExceptionStatusCode.emailAlreadyInUse); } catch (e) { fail(e.toString()); } @@ -275,7 +275,7 @@ void runInstanceTests() { email: '!!!!!', password: '123456'); fail("Should have thrown FirebaseAuthException"); } on FirebaseAuthException catch (e) { - expect(e.code, equals('invalid-email')); + expect(e.statusCode, AuthExceptionStatusCode.invalidEmail); } catch (e) { fail(e.toString()); } @@ -288,7 +288,7 @@ void runInstanceTests() { email: generateRandomEmail(), password: '1'); fail("Should have thrown FirebaseAuthException"); } on FirebaseAuthException catch (e) { - expect(e.code, equals('weak-password')); + expect(e.statusCode, AuthExceptionStatusCode.weakPassword); } catch (e) { fail(e.toString()); } @@ -315,7 +315,7 @@ void runInstanceTests() { await auth.fetchSignInMethodsForEmail('foobar'); fail('Should have thrown'); } on FirebaseAuthException catch (e) { - expect(e.code, equals("invalid-email")); + expect(e.statusCode, AuthExceptionStatusCode.invalidEmail); } catch (e) { fail(e.toString()); } @@ -375,7 +375,7 @@ void runInstanceTests() { await auth.sendPasswordResetEmail(email: 'does-not-exist@bar.com'); fail('Should have thrown'); } on FirebaseAuthException catch (e) { - expect(e.code, equals('user-not-found')); + expect(e.statusCode, AuthExceptionStatusCode.userNotFound); } catch (e) { fail(e.toString()); } @@ -720,7 +720,8 @@ void runInstanceTests() { Exception e = await getError(); expect(e, isA()); FirebaseAuthException exception = e as FirebaseAuthException; - expect(exception.code, equals('invalid-phone-number')); + expect( + exception.statusCode, AuthExceptionStatusCode.invalidPhoneNumber); }); test('should auto verify phone number', () async { diff --git a/packages/firebase_auth/firebase_auth/example/test_driver/test_utils.dart b/packages/firebase_auth/firebase_auth/example/test_driver/test_utils.dart index 7ee466decd38..30f3160ed947 100644 --- a/packages/firebase_auth/firebase_auth/example/test_driver/test_utils.dart +++ b/packages/firebase_auth/firebase_auth/example/test_driver/test_utils.dart @@ -50,7 +50,7 @@ void ensureSignedIn(testEmail) async { await auth.createUserWithEmailAndPassword( email: testEmail, password: TEST_PASSWORD); } on FirebaseAuthException catch (e) { - if (e.code == 'email-already-in-use') { + if (e.statusCode == AuthExceptionStatusCode.emailAlreadyInUse) { await auth.signInWithEmailAndPassword( email: testEmail, password: TEST_PASSWORD); } diff --git a/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart b/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart index 518e675bd8b8..13ef2aadec80 100644 --- a/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart +++ b/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart @@ -126,7 +126,7 @@ void runUserTests() { )); } on FirebaseAuthException catch (e) { // Assertions - expect(e.code, 'email-already-in-use'); + expect(e.statusCode, AuthExceptionStatusCode.emailAlreadyInUse); expect(e.message, 'The email address is already in use by another account.'); @@ -190,7 +190,7 @@ void runUserTests() { PhoneAuthProvider.credential( verificationId: 'test', smsCode: 'test')); } on FirebaseAuthException catch (e) { - expect(e.code, equals("invalid-verification-id")); + expect(e.statusCode, equals("invalid-verification-id")); expect( e.message, equals( @@ -239,7 +239,7 @@ void runUserTests() { await auth.currentUser.reauthenticateWithCredential(credential); } on FirebaseAuthException catch (e) { // Assertions - expect(e.code, equals("user-mismatch")); + expect(e.statusCode, AuthExceptionStatusCode.userMismatch); expect( e.message, equals( @@ -293,7 +293,7 @@ void runUserTests() { await auth.currentUser.reauthenticateWithCredential(credential); } on FirebaseAuthException catch (e) { // Assertions - expect(e.code, equals("invalid-email")); + expect(e.statusCode, AuthExceptionStatusCode.invalidEmail); expect(e.message, equals("The email address is badly formatted.")); return; } catch (e) { @@ -315,7 +315,7 @@ void runUserTests() { await auth.currentUser.reauthenticateWithCredential(credential); } on FirebaseAuthException catch (e) { // Assertions - expect(e.code, equals("wrong-password")); + expect(e.statusCode, AuthExceptionStatusCode.wrongPassword); expect( e.message, equals( @@ -415,7 +415,7 @@ void runUserTests() { try { await auth.currentUser.unlink("invalid"); } on FirebaseAuthException catch (e) { - expect(e.code, 'no-such-provider'); + expect(e.statusCode, AuthExceptionStatusCode.noSuchProvider); expect(e.message, 'User was not linked to an account with the given provider.'); return; @@ -433,7 +433,7 @@ void runUserTests() { try { await auth.currentUser.unlink(EmailAuthProvider.PROVIDER_ID); } on FirebaseAuthException catch (e) { - expect(e.code, 'no-such-provider'); + expect(e.statusCode, AuthExceptionStatusCode.noSuchProvider); expect(e.message, 'User was not linked to an account with the given provider.'); return; @@ -488,7 +488,7 @@ void runUserTests() { // Update user password await auth.currentUser.updatePassword('weak'); } on FirebaseAuthException catch (e) { - expect(e.code, 'weak-password'); + expect(e.statusCode, AuthExceptionStatusCode.weakPassword); expect(e.message, 'Password should be at least 6 characters'); return; } catch (e) { @@ -624,7 +624,7 @@ void runUserTests() { await auth.currentUser.updatePhoneNumber(PhoneAuthProvider.credential( verificationId: "invalid", smsCode: TEST_SMS_CODE)); } on FirebaseAuthException catch (e) { - expect(e.code, "invalid-verification-id"); + expect(e.statusCode, AuthExceptionStatusCode.invalidVerificationId); expect(e.message, "The verification ID used to create the phone auth credential is invalid."); return; @@ -665,9 +665,10 @@ void runUserTests() { await auth.currentUser.updatePhoneNumber(PhoneAuthProvider.credential( verificationId: "", smsCode: TEST_SMS_CODE)); } on FirebaseAuthException catch (e) { - print(e.code); + // ignore: unnecessary_cast + print((e as FirebaseException).code); print(e.message); - expect(e.code, "invalid-verification-id"); + expect(e.statusCode, AuthExceptionStatusCode.invalidVerificationId); expect(e.message, "The verification ID used to create the phone auth credential is invalid."); return; @@ -761,7 +762,7 @@ void runUserTests() { await user.delete(); } on FirebaseAuthException catch (e) { // Assertions - expect(e.code, 'no-current-user'); + expect(e.statusCode, AuthExceptionStatusCode.noCurrentUser); expect(e.message, 'No user currently signed in.'); return; diff --git a/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart b/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart index 45323fe2844c..a69fa2ac23b4 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart @@ -2,6 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'package:firebase_core/firebase_core.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart'; import 'package:firebase_auth_platform_interface/src/method_channel/utils/exception.dart'; @@ -52,7 +53,8 @@ void main() { FirebaseAuthException result = platformExceptionToFirebaseAuthException(platformException); - expect(result.code, equals('unknown')); + // ignore: unnecessary_cast + expect((result as FirebaseException).code, equals('unknown')); expect(result.message, equals('PlatformException Message')); expect(result.email, isNull); @@ -79,7 +81,8 @@ void main() { FirebaseAuthException result = platformExceptionToFirebaseAuthException(platformException); - expect(result.code, equals('A Known Code')); + // ignore: unnecessary_cast + expect((result as FirebaseException).code, equals('A Known Code')); expect(result.message, equals('A Known Message')); expect(result.email, 'test@email.com'); @@ -96,7 +99,8 @@ void main() { FirebaseAuthException result = platformExceptionToFirebaseAuthException(platformException); - expect(result.code, equals('unknown')); + // ignore: unnecessary_cast + expect((result as FirebaseException).code, equals('unknown')); expect(result.message, equals('a message')); expect(result.email, null); @@ -111,7 +115,8 @@ void main() { FirebaseAuthException result = platformExceptionToFirebaseAuthException(platformException); - expect(result.code, equals('unknown')); + // ignore: unnecessary_cast + expect((result as FirebaseException).code, equals('unknown')); expect(result.message, equals('a message')); expect(result.email, isNull); @@ -128,7 +133,8 @@ void main() { FirebaseAuthException result = platformExceptionToFirebaseAuthException(platformException); - expect(result.code, equals('A Known Code')); + // ignore: unnecessary_cast + expect((result as FirebaseException).code, equals('A Known Code')); expect(result.message, equals('A Known Message')); expect(result.email, 'test@email.com'); From cfa240a4cc6874c2baad72b8a8b496bdefb7abbf Mon Sep 17 00:00:00 2001 From: Kristian Balaj Date: Mon, 31 Aug 2020 20:35:24 +0200 Subject: [PATCH 6/8] test(firebase_auth): test fix --- .../firebase_auth/example/test_driver/user_e2e.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart b/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart index 13ef2aadec80..063e10fb35cf 100644 --- a/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart +++ b/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart @@ -190,7 +190,7 @@ void runUserTests() { PhoneAuthProvider.credential( verificationId: 'test', smsCode: 'test')); } on FirebaseAuthException catch (e) { - expect(e.statusCode, equals("invalid-verification-id")); + expect(e.statusCode, AuthExceptionStatusCode.invalidVerificationId); expect( e.message, equals( From b7b6c1f301e8a14ccd8bc80278f1e63ff133b472 Mon Sep 17 00:00:00 2001 From: Kristian Balaj Date: Mon, 31 Aug 2020 20:59:54 +0200 Subject: [PATCH 7/8] fix(firebase_auth): authException status code wasn't returning noCurrentUser status code --- .../lib/src/firebase_auth_exception.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart index 6c813fd13f97..02fbc5db29ab 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart @@ -70,6 +70,8 @@ class FirebaseAuthException extends FirebaseException implements Exception { return AuthExceptionStatusCode.userMismatch; case 'no-such-provider': return AuthExceptionStatusCode.noSuchProvider; + case 'no-current-user': + return AuthExceptionStatusCode.noCurrentUser; default: return AuthExceptionStatusCode.unknown; } From dd80e7a4261f3ad0349f54bcfd6484c350f722f2 Mon Sep 17 00:00:00 2001 From: ehesp Date: Mon, 28 Sep 2020 14:27:57 +0100 Subject: [PATCH 8/8] add additional status codes --- .../example/lib/signin_page.dart | 2 +- .../example/test_driver/instance_e2e.dart | 27 ++++--- .../example/test_driver/test_utils.dart | 2 +- .../example/test_driver/user_e2e.dart | 22 +++--- .../lib/src/auth_exception_status_code.dart | 74 ++++++++++++++----- .../lib/src/firebase_auth_exception.dart | 29 ++++++-- 6 files changed, 111 insertions(+), 45 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/example/lib/signin_page.dart b/packages/firebase_auth/firebase_auth/example/lib/signin_page.dart index dd1033e2ea67..444d101e19a0 100644 --- a/packages/firebase_auth/firebase_auth/example/lib/signin_page.dart +++ b/packages/firebase_auth/firebase_auth/example/lib/signin_page.dart @@ -435,7 +435,7 @@ class _PhoneSignInSectionState extends State<_PhoneSignInSection> { (FirebaseAuthException authException) { setState(() { _message = - 'Phone number verification failed. Code: ${authException.statusCode}. Message: ${authException.message}'; + 'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}'; }); }; diff --git a/packages/firebase_auth/firebase_auth/example/test_driver/instance_e2e.dart b/packages/firebase_auth/firebase_auth/example/test_driver/instance_e2e.dart index c9fbb6ca0855..b25a26b2937a 100644 --- a/packages/firebase_auth/firebase_auth/example/test_driver/instance_e2e.dart +++ b/packages/firebase_auth/firebase_auth/example/test_driver/instance_e2e.dart @@ -193,8 +193,9 @@ void runInstanceTests() { try { await auth.applyActionCode('!!!!!!'); fail("Should have thrown"); - } on FirebaseException catch (e) { + } on FirebaseAuthException catch (e) { expect(e.code, equals("invalid-action-code")); + expect(e.status, AuthExceptionStatusCode.invalidActionCode); } catch (e) { fail(e.toString()); } @@ -206,8 +207,9 @@ void runInstanceTests() { try { await auth.checkActionCode('!!!!!!'); fail('Should have thrown'); - } on FirebaseException catch (e) { + } on FirebaseAuthException catch (e) { expect(e.code, equals("invalid-action-code")); + expect(e.status, AuthExceptionStatusCode.invalidActionCode); } catch (e) { fail(e.toString()); } @@ -220,8 +222,9 @@ void runInstanceTests() { await auth.confirmPasswordReset( code: '!!!!!!', newPassword: 'thingamajig'); fail('Should have thrown'); - } on FirebaseException catch (e) { + } on FirebaseAuthException catch (e) { expect(e.code, equals("invalid-action-code")); + expect(e.status, AuthExceptionStatusCode.invalidActionCode); } catch (e) { fail((e.toString())); } @@ -262,7 +265,8 @@ void runInstanceTests() { email: regularTestEmail, password: '123456'); fail("Should have thrown FirebaseAuthException"); } on FirebaseAuthException catch (e) { - expect(e.statusCode, AuthExceptionStatusCode.emailAlreadyInUse); + expect(e.code, equals('email-already-in-use')); + expect(e.status, AuthExceptionStatusCode.emailAlreadyInUse); } catch (e) { fail(e.toString()); } @@ -275,7 +279,8 @@ void runInstanceTests() { email: '!!!!!', password: '123456'); fail("Should have thrown FirebaseAuthException"); } on FirebaseAuthException catch (e) { - expect(e.statusCode, AuthExceptionStatusCode.invalidEmail); + expect(e.code, equals('invalid-email')); + expect(e.status, AuthExceptionStatusCode.invalidEmail); } catch (e) { fail(e.toString()); } @@ -288,7 +293,8 @@ void runInstanceTests() { email: generateRandomEmail(), password: '1'); fail("Should have thrown FirebaseAuthException"); } on FirebaseAuthException catch (e) { - expect(e.statusCode, AuthExceptionStatusCode.weakPassword); + expect(e.code, equals('weak-password')); + expect(e.status, AuthExceptionStatusCode.weakPassword); } catch (e) { fail(e.toString()); } @@ -315,7 +321,8 @@ void runInstanceTests() { await auth.fetchSignInMethodsForEmail('foobar'); fail('Should have thrown'); } on FirebaseAuthException catch (e) { - expect(e.statusCode, AuthExceptionStatusCode.invalidEmail); + expect(e.code, equals('invalid-email')); + expect(e.status, AuthExceptionStatusCode.invalidEmail); } catch (e) { fail(e.toString()); } @@ -375,7 +382,8 @@ void runInstanceTests() { await auth.sendPasswordResetEmail(email: 'does-not-exist@bar.com'); fail('Should have thrown'); } on FirebaseAuthException catch (e) { - expect(e.statusCode, AuthExceptionStatusCode.userNotFound); + expect(e.code, equals('user-not-found')); + expect(e.status, AuthExceptionStatusCode.userNotFound); } catch (e) { fail(e.toString()); } @@ -720,8 +728,7 @@ void runInstanceTests() { Exception e = await getError(); expect(e, isA()); FirebaseAuthException exception = e as FirebaseAuthException; - expect( - exception.statusCode, AuthExceptionStatusCode.invalidPhoneNumber); + expect(exception.status, AuthExceptionStatusCode.invalidPhoneNumber); }); test('should auto verify phone number', () async { diff --git a/packages/firebase_auth/firebase_auth/example/test_driver/test_utils.dart b/packages/firebase_auth/firebase_auth/example/test_driver/test_utils.dart index 30f3160ed947..fce25e2250a0 100644 --- a/packages/firebase_auth/firebase_auth/example/test_driver/test_utils.dart +++ b/packages/firebase_auth/firebase_auth/example/test_driver/test_utils.dart @@ -50,7 +50,7 @@ void ensureSignedIn(testEmail) async { await auth.createUserWithEmailAndPassword( email: testEmail, password: TEST_PASSWORD); } on FirebaseAuthException catch (e) { - if (e.statusCode == AuthExceptionStatusCode.emailAlreadyInUse) { + if (e.status == AuthExceptionStatusCode.emailAlreadyInUse) { await auth.signInWithEmailAndPassword( email: testEmail, password: TEST_PASSWORD); } diff --git a/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart b/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart index 063e10fb35cf..482209e76059 100644 --- a/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart +++ b/packages/firebase_auth/firebase_auth/example/test_driver/user_e2e.dart @@ -126,7 +126,7 @@ void runUserTests() { )); } on FirebaseAuthException catch (e) { // Assertions - expect(e.statusCode, AuthExceptionStatusCode.emailAlreadyInUse); + expect(e.status, AuthExceptionStatusCode.emailAlreadyInUse); expect(e.message, 'The email address is already in use by another account.'); @@ -190,7 +190,7 @@ void runUserTests() { PhoneAuthProvider.credential( verificationId: 'test', smsCode: 'test')); } on FirebaseAuthException catch (e) { - expect(e.statusCode, AuthExceptionStatusCode.invalidVerificationId); + expect(e.status, AuthExceptionStatusCode.invalidVerificationId); expect( e.message, equals( @@ -239,7 +239,7 @@ void runUserTests() { await auth.currentUser.reauthenticateWithCredential(credential); } on FirebaseAuthException catch (e) { // Assertions - expect(e.statusCode, AuthExceptionStatusCode.userMismatch); + expect(e.status, AuthExceptionStatusCode.userMismatch); expect( e.message, equals( @@ -293,7 +293,7 @@ void runUserTests() { await auth.currentUser.reauthenticateWithCredential(credential); } on FirebaseAuthException catch (e) { // Assertions - expect(e.statusCode, AuthExceptionStatusCode.invalidEmail); + expect(e.status, AuthExceptionStatusCode.invalidEmail); expect(e.message, equals("The email address is badly formatted.")); return; } catch (e) { @@ -315,7 +315,7 @@ void runUserTests() { await auth.currentUser.reauthenticateWithCredential(credential); } on FirebaseAuthException catch (e) { // Assertions - expect(e.statusCode, AuthExceptionStatusCode.wrongPassword); + expect(e.status, AuthExceptionStatusCode.wrongPassword); expect( e.message, equals( @@ -415,7 +415,7 @@ void runUserTests() { try { await auth.currentUser.unlink("invalid"); } on FirebaseAuthException catch (e) { - expect(e.statusCode, AuthExceptionStatusCode.noSuchProvider); + expect(e.status, AuthExceptionStatusCode.noSuchProvider); expect(e.message, 'User was not linked to an account with the given provider.'); return; @@ -433,7 +433,7 @@ void runUserTests() { try { await auth.currentUser.unlink(EmailAuthProvider.PROVIDER_ID); } on FirebaseAuthException catch (e) { - expect(e.statusCode, AuthExceptionStatusCode.noSuchProvider); + expect(e.status, AuthExceptionStatusCode.noSuchProvider); expect(e.message, 'User was not linked to an account with the given provider.'); return; @@ -488,7 +488,7 @@ void runUserTests() { // Update user password await auth.currentUser.updatePassword('weak'); } on FirebaseAuthException catch (e) { - expect(e.statusCode, AuthExceptionStatusCode.weakPassword); + expect(e.status, AuthExceptionStatusCode.weakPassword); expect(e.message, 'Password should be at least 6 characters'); return; } catch (e) { @@ -624,7 +624,7 @@ void runUserTests() { await auth.currentUser.updatePhoneNumber(PhoneAuthProvider.credential( verificationId: "invalid", smsCode: TEST_SMS_CODE)); } on FirebaseAuthException catch (e) { - expect(e.statusCode, AuthExceptionStatusCode.invalidVerificationId); + expect(e.status, AuthExceptionStatusCode.invalidVerificationId); expect(e.message, "The verification ID used to create the phone auth credential is invalid."); return; @@ -668,7 +668,7 @@ void runUserTests() { // ignore: unnecessary_cast print((e as FirebaseException).code); print(e.message); - expect(e.statusCode, AuthExceptionStatusCode.invalidVerificationId); + expect(e.status, AuthExceptionStatusCode.invalidVerificationId); expect(e.message, "The verification ID used to create the phone auth credential is invalid."); return; @@ -762,7 +762,7 @@ void runUserTests() { await user.delete(); } on FirebaseAuthException catch (e) { // Assertions - expect(e.statusCode, AuthExceptionStatusCode.noCurrentUser); + expect(e.status, AuthExceptionStatusCode.noCurrentUser); expect(e.message, 'No user currently signed in.'); return; diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart index b302281b33bb..f131b148f85c 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/auth_exception_status_code.dart @@ -1,50 +1,90 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + /// The codes of the auth exceptions. enum AuthExceptionStatusCode { - /// Used if the email address is not valid. + /// Thrown if the email address is not valid. invalidEmail, - /// Used if the user corresponding to the given email has been disabled. + /// Thrown if the user corresponding to the given email has been disabled. userDisabled, - /// Used if there is no user corresponding to the given email. + /// Thrown if there is no user corresponding to the given email. userNotFound, - /// Used if the password is invalid for the given email, or the account corresponding to the email does not have a password set. + /// Thrown if the password is invalid for the given email, or the account corresponding to the email does not have a password set. wrongPassword, - /// Used if the Firebase Authentication quota is reached. + /// Thrown if the Firebase Authentication quota is reached. tooManyRequests, - /// Used if specific auth provider is not enabled. + /// Thrown if specific auth provider is not enabled. operationNotAllowed, - /// Used if the email exists for multiple Firebase user's providers. + /// Thrown if the email exists for multiple Firebase user's providers. accountExistsWithDifferentCredential, - /// Used if the request failed due to network issues. + /// Thrown if the request failed due to network issues. networkRequestFailed, - /// Used if a user being created already exists. + /// Thrown if a user being created already exists. emailAlreadyInUse, - /// Used if the request to create a user has a weak password. + /// Thrown if the request to create a user has a weak password. weakPassword, - /// Used if the phone verification fails with an invalid phone number. + /// Thrown if the phone verification fails with an invalid phone number. invalidPhoneNumber, - /// Used if the verification ID used to create the phone auth credential is invalid. + /// Thrown if the verification ID used to create the phone auth credential is invalid. invalidVerificationId, - /// Used if the supplied credentials do not correspond to the previously signed in user. + /// Thrown if the supplied credentials do not correspond to the previously signed in user. userMismatch, - /// Used if the user was not linked to an account with the given provider. + /// Thrown if the user does not have this provider linked or when the provider ID given does not exist. noSuchProvider, - /// Used if there is no user currently signed in. + /// Thrown if there is no user currently signed in. noCurrentUser, - /// Used if the status is unknown. - unknown + /// Thrown if the supplied action code has expired. + expiredActionCode, + + /// Thrown if the supplied action code is not a valid format. + invalidActionCode, + + /// Thrown if the custom token is for a different Firebase App. + customTokenMismatch, + + /// Thrown if the custom token format is incorrect. + invalidCustomToken, + + /// Thrown if the credential is a [PhoneAuthProvider.credential] and the verification + /// code of the credential is not valid. + invalidVerificationCode, + + /// Thrown if the credential is malformed or has expired. + invalidCredential, + + /// if the user's last sign-in time does not meet the security threshold. + /// + /// Use [User.reauthenticateWithCredential] to resolve. This does not apply if the user is anonymous. + requiresRecentLogin, + + /// Thrown if the provider has already been linked to the user. + /// + /// This error is thrown even if this is not the same provider's account that is currently linked to the user. + providerAlreadyLinked, + + /// Thrown if the provider's credential is not valid. + /// + /// This can happen if it has already expired when calling link, or if it used + /// invalid token(s). See the Firebase documentation for your provider, and make + /// sure you pass in the correct parameters to the credential method. + credentialAlreadyInUse, + + /// Thrown if the status is unknown. + unknown, } diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart index 02fbc5db29ab..270c7dbc2f96 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/firebase_auth_exception.dart @@ -14,15 +14,15 @@ class FirebaseAuthException extends FirebaseException implements Exception { @protected FirebaseAuthException( {@required this.message, - String code, + this.code, this.email, this.credential, this.phoneNumber, this.tenantId}) : super(plugin: 'firebase_auth', message: message, code: code); - @Deprecated('Deprecated in favor of `.statusCode`.') - String get code => super.code; + /// Unique error code. + final String code; /// Complete error message. final String message; @@ -39,8 +39,8 @@ class FirebaseAuthException extends FirebaseException implements Exception { /// The tenant ID being used for sign-in/linking. final String tenantId; - /// The error code. - AuthExceptionStatusCode get statusCode { + /// The error status code. + AuthExceptionStatusCode get status { switch (super.code) { case 'invalid-email': return AuthExceptionStatusCode.invalidEmail; @@ -72,6 +72,25 @@ class FirebaseAuthException extends FirebaseException implements Exception { return AuthExceptionStatusCode.noSuchProvider; case 'no-current-user': return AuthExceptionStatusCode.noCurrentUser; + case 'expired-action-code': + return AuthExceptionStatusCode.expiredActionCode; + case 'invalid-action-code': + return AuthExceptionStatusCode.invalidActionCode; + case 'custom-token-mismatch': + return AuthExceptionStatusCode.customTokenMismatch; + case 'invalid-custom-token': + return AuthExceptionStatusCode.invalidCustomToken; + case 'invalid-verification-code': + return AuthExceptionStatusCode.invalidVerificationCode; + case 'invalid-credential': + return AuthExceptionStatusCode.invalidCredential; + case 'requires-recent-login': + return AuthExceptionStatusCode.requiresRecentLogin; + case 'provider-already-linked': + return AuthExceptionStatusCode.providerAlreadyLinked; + case 'credential-already-in-use': + return AuthExceptionStatusCode.credentialAlreadyInUse; + case 'unknown': default: return AuthExceptionStatusCode.unknown; }