diff --git a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt index a59e485660de..71d5f7bda6fc 100644 --- a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt +++ b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt @@ -184,10 +184,14 @@ class FlutterFirebaseAuthPlugin : password: String, callback: (Result) -> Unit ) { - getAuthFromPigeon(app).createUserWithEmailAndPassword(email, password).addOnCompleteListener { - task -> - completeAuthResult(task, callback) - } + val task = + try { + getAuthFromPigeon(app).createUserWithEmailAndPassword(email, password) + } catch (e: Exception) { + callback(Result.failure(FlutterFirebaseAuthPluginException.parserExceptionToFlutter(e))) + return + } + task.addOnCompleteListener { completed -> completeAuthResult(completed, callback) } } override fun signInAnonymously( @@ -230,10 +234,14 @@ class FlutterFirebaseAuthPlugin : password: String, callback: (Result) -> Unit ) { - getAuthFromPigeon(app).signInWithEmailAndPassword(email, password).addOnCompleteListener { task - -> - completeAuthResult(task, callback) - } + val task = + try { + getAuthFromPigeon(app).signInWithEmailAndPassword(email, password) + } catch (e: Exception) { + callback(Result.failure(FlutterFirebaseAuthPluginException.parserExceptionToFlutter(e))) + return + } + task.addOnCompleteListener { completed -> completeAuthResult(completed, callback) } } override fun signInWithEmailLink( @@ -308,11 +316,16 @@ class FlutterFirebaseAuthPlugin : ) { val firebaseAuth = getAuthFromPigeon(app) val task = - if (actionCodeSettings == null) { - firebaseAuth.sendPasswordResetEmail(email) - } else { - firebaseAuth.sendPasswordResetEmail( - email, PigeonParser.getActionCodeSettings(actionCodeSettings)) + try { + if (actionCodeSettings == null) { + firebaseAuth.sendPasswordResetEmail(email) + } else { + firebaseAuth.sendPasswordResetEmail( + email, PigeonParser.getActionCodeSettings(actionCodeSettings)) + } + } catch (e: Exception) { + callback(Result.failure(FlutterFirebaseAuthPluginException.parserExceptionToFlutter(e))) + return } task.addOnCompleteListener { completed -> completeVoid(completed, callback) } } diff --git a/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart b/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart index c81d5decf256..b48faa4c855a 100644 --- a/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart +++ b/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart @@ -531,6 +531,41 @@ void main() { fail(e.toString()); } }); + + test( + 'fails with a FirebaseAuthException if the email or password is empty', + () async { + await expectLater( + FirebaseAuth.instance.createUserWithEmailAndPassword( + email: '', + password: testPassword, + ), + throwsA( + isA().having( + (e) => e.code, + 'code', + 'unknown', + ), + ), + ); + await expectLater( + FirebaseAuth.instance.createUserWithEmailAndPassword( + email: generateRandomEmail(), + password: '', + ), + throwsA( + isA().having( + (e) => e.code, + 'code', + 'unknown', + ), + ), + ); + }, + // Only the Android SDK rejects empty values itself; elsewhere the + // error comes from the backend and differs per method. + skip: kIsWeb || defaultTargetPlatform != TargetPlatform.android, + ); }); group('isSignInWithEmailLink()', () { @@ -592,6 +627,23 @@ void main() { fail(e.toString()); } }); + + test( + 'fails with a FirebaseAuthException if the email is empty', + () async { + await expectLater( + FirebaseAuth.instance.sendPasswordResetEmail(email: ''), + throwsA( + isA().having( + (e) => e.code, + 'code', + 'unknown', + ), + ), + ); + }, + skip: kIsWeb || defaultTargetPlatform != TargetPlatform.android, + ); }, skip: !kIsWeb && Platform.isWindows); group('sendSignInLinkToEmail()', () { @@ -908,6 +960,40 @@ void main() { fail(e.toString()); } }); + + test( + 'throws a FirebaseAuthException if the email or password is empty', + () async { + await expectLater( + FirebaseAuth.instance.signInWithEmailAndPassword( + email: '', + password: testPassword, + ), + throwsA( + isA().having( + (e) => e.code, + 'code', + 'unknown', + ), + ), + ); + await expectLater( + FirebaseAuth.instance.signInWithEmailAndPassword( + email: testEmail, + password: '', + ), + throwsA( + isA().having( + (e) => e.code, + 'code', + 'unknown', + ), + ), + ); + }, + skip: kIsWeb || defaultTargetPlatform != TargetPlatform.android, + ); + test( 'should not throw error when app is deleted and reinit with same app name', () async {