From e5d9c6b0109a55a8c72d2aca4fdebdd136e26dd3 Mon Sep 17 00:00:00 2001 From: Guillaume Bernos Date: Mon, 13 Mar 2023 11:06:41 +0100 Subject: [PATCH 1/3] feat(auth): improve error handling when Email enumeration feature is on --- .../firebase_auth/example/lib/auth.dart | 4 +--- .../ios/Classes/FLTFirebaseAuthPlugin.m | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/example/lib/auth.dart b/packages/firebase_auth/firebase_auth/example/lib/auth.dart index 0ef850357997..e8b6ac781699 100644 --- a/packages/firebase_auth/firebase_auth/example/lib/auth.dart +++ b/packages/firebase_auth/firebase_auth/example/lib/auth.dart @@ -448,14 +448,12 @@ class _AuthGateState extends State { setState(() { error = '$e'; }); - } finally { - setIsLoading(); } + setIsLoading(); } Future _emailAndPassword() async { if (formKey.currentState?.validate() ?? false) { - setIsLoading(); if (mode == AuthMode.login) { await auth.signInWithEmailAndPassword( email: emailController.text, diff --git a/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m b/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m index 3545a1d6fd8d..50e23bb08f7d 100644 --- a/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m +++ b/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m @@ -743,6 +743,20 @@ - (void)signInWithCustomToken:(id)arguments }]; } + +- (void)handleInternalError:(id)arguments + withResult:(FLTFirebaseMethodCallResult *)result + withError:(NSError *)error { + const NSError *underlyingError = error.userInfo[@"NSUnderlyingError"]; + if (underlyingError != nil) { + const NSDictionary *details = underlyingError.userInfo[@"FIRAuthErrorUserInfoDeserializedResponseKey"]; + if (details != nil && details[@"message"] != nil) { + NSLog(@"%@", details[@"message"]); + } + } +} + + - (void)handleMultiFactorError:(id)arguments withResult:(FLTFirebaseMethodCallResult *)result withError:(NSError *_Nullable)error { @@ -801,6 +815,9 @@ - (void)signInWithEmailAndPassword:(id)arguments if (error != nil) { if (error.code == FIRAuthErrorCodeSecondFactorRequired) { [self handleMultiFactorError:arguments withResult:result withError:error]; + } + else if (error.code == FIRAuthErrorCodeInternalError) { + [self handleInternalError:arguments withResult:result withError:error]; } else { result.error(nil, nil, nil, error); } From 3c61426b6f20edb58cb52e08fc7816062649f32e Mon Sep 17 00:00:00 2001 From: Guillaume Bernos Date: Tue, 14 Mar 2023 09:56:17 +0100 Subject: [PATCH 2/3] feat(auth): improve error handling when Email enumeration feature is on --- .../ios/Classes/FLTFirebaseAuthPlugin.m | 6 ++--- .../src/method_channel/utils/exception.dart | 22 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m b/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m index 50e23bb08f7d..1a3973982ae0 100644 --- a/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m +++ b/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m @@ -750,10 +750,10 @@ - (void)handleInternalError:(id)arguments const NSError *underlyingError = error.userInfo[@"NSUnderlyingError"]; if (underlyingError != nil) { const NSDictionary *details = underlyingError.userInfo[@"FIRAuthErrorUserInfoDeserializedResponseKey"]; - if (details != nil && details[@"message"] != nil) { - NSLog(@"%@", details[@"message"]); - } + result.error(nil, nil, details, underlyingError); + return; } + result.error(nil, nil, nil, error); } diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart index 652857fe940c..2d7a9e255a1f 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart @@ -64,18 +64,24 @@ FirebaseException platformExceptionToFirebaseAuthException( message = details['message'] ?? message; - if (details['additionalData'] != null) { - if (details['additionalData']['authCredential'] != null) { + final additionalData = details['additionalData']; + + if (additionalData != null) { + if (additionalData['authCredential'] != null) { credential = AuthCredential( - providerId: details['additionalData']['authCredential']['providerId'], - signInMethod: details['additionalData']['authCredential'] - ['signInMethod'], - token: details['additionalData']['authCredential']['token'], + providerId: additionalData['authCredential']['providerId'], + signInMethod: additionalData['authCredential']['signInMethod'], + token: additionalData['authCredential']['token'], ); } - if (details['additionalData']['email'] != null) { - email = details['additionalData']['email']; + if (additionalData['email'] != null) { + email = additionalData['email']; + } + + // This code happens when using Enumerate Email protection + if (additionalData["message"] == "INVALID_LOGIN_CREDENTIALS") { + code = "INVALID_LOGIN_CREDENTIALS"; } } } From 24dc3b3838082a6dd6db6bacc78da2f8b07aaa0c Mon Sep 17 00:00:00 2001 From: Guillaume Bernos Date: Tue, 14 Mar 2023 10:20:15 +0100 Subject: [PATCH 3/3] feat(auth): improve error handling when Email enumeration feature is on --- .../ios/Classes/FLTFirebaseAuthPlugin.m | 28 +++++++++++-------- .../src/method_channel/utils/exception.dart | 11 ++++---- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m b/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m index 1a3973982ae0..a9ae884ef3b7 100644 --- a/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m +++ b/packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m @@ -494,6 +494,8 @@ - (void)signInWithCredential:(id)arguments } else { if (error.code == FIRAuthErrorCodeSecondFactorRequired) { [self handleMultiFactorError:arguments withResult:result withError:error]; + } else if (error.code == FIRAuthErrorCodeInternalError) { + [self handleInternalError:arguments withResult:result withError:error]; } else { result.error(nil, nil, nil, error); } @@ -734,6 +736,8 @@ - (void)signInWithCustomToken:(id)arguments if (error != nil) { if (error.code == FIRAuthErrorCodeSecondFactorRequired) { [self handleMultiFactorError:arguments withResult:result withError:error]; + } else if (error.code == FIRAuthErrorCodeInternalError) { + [self handleInternalError:arguments withResult:result withError:error]; } else { result.error(nil, nil, nil, error); } @@ -743,20 +747,19 @@ - (void)signInWithCustomToken:(id)arguments }]; } - - (void)handleInternalError:(id)arguments - withResult:(FLTFirebaseMethodCallResult *)result + withResult:(FLTFirebaseMethodCallResult *)result withError:(NSError *)error { - const NSError *underlyingError = error.userInfo[@"NSUnderlyingError"]; - if (underlyingError != nil) { - const NSDictionary *details = underlyingError.userInfo[@"FIRAuthErrorUserInfoDeserializedResponseKey"]; - result.error(nil, nil, details, underlyingError); - return; - } - result.error(nil, nil, nil, error); + const NSError *underlyingError = error.userInfo[@"NSUnderlyingError"]; + if (underlyingError != nil) { + const NSDictionary *details = + underlyingError.userInfo[@"FIRAuthErrorUserInfoDeserializedResponseKey"]; + result.error(nil, nil, details, underlyingError); + return; + } + result.error(nil, nil, nil, error); } - - (void)handleMultiFactorError:(id)arguments withResult:(FLTFirebaseMethodCallResult *)result withError:(NSError *_Nullable)error { @@ -815,8 +818,7 @@ - (void)signInWithEmailAndPassword:(id)arguments if (error != nil) { if (error.code == FIRAuthErrorCodeSecondFactorRequired) { [self handleMultiFactorError:arguments withResult:result withError:error]; - } - else if (error.code == FIRAuthErrorCodeInternalError) { + } else if (error.code == FIRAuthErrorCodeInternalError) { [self handleInternalError:arguments withResult:result withError:error]; } else { result.error(nil, nil, nil, error); @@ -836,6 +838,8 @@ - (void)signInWithEmailLink:(id)arguments if (error != nil) { if (error.code == FIRAuthErrorCodeSecondFactorRequired) { [self handleMultiFactorError:arguments withResult:result withError:error]; + } else if (error.code == FIRAuthErrorCodeInternalError) { + [self handleInternalError:arguments withResult:result withError:error]; } else { result.error(nil, nil, nil, error); } diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart index 2d7a9e255a1f..b8285a908f4f 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart @@ -78,11 +78,12 @@ FirebaseException platformExceptionToFirebaseAuthException( if (additionalData['email'] != null) { email = additionalData['email']; } - - // This code happens when using Enumerate Email protection - if (additionalData["message"] == "INVALID_LOGIN_CREDENTIALS") { - code = "INVALID_LOGIN_CREDENTIALS"; - } + } + // This code happens when using Enumerate Email protection + // The error code is only returned in a String on Android + if (additionalData?['message'] == 'INVALID_LOGIN_CREDENTIALS' || + (message?.contains('INVALID_LOGIN_CREDENTIALS') ?? false)) { + code = 'INVALID_LOGIN_CREDENTIALS'; } } return FirebaseAuthException(