From 2d1894272e2110c20ad6292a92a25d82a3680f3a Mon Sep 17 00:00:00 2001 From: Simon Lightfoot Date: Fri, 20 Jul 2018 01:49:41 +0100 Subject: [PATCH] Added `updatePassword` functionality to firebase_auth plugin. Move `updateEmail` and `updateProfile` from FirebaseAuth to FirebaseUser. Minor cleani-up in Java. Updated tests. Fix dartfmt issue. Fix merge conflicts. Deprecated old methods. Updated Changelog. Review changes. --- packages/firebase_auth/CHANGELOG.md | 8 ++- .../firebaseauth/FirebaseAuthPlugin.java | 63 ++++++++----------- packages/firebase_auth/example/lib/main.dart | 2 +- .../ios/Classes/FirebaseAuthPlugin.m | 18 ++++-- packages/firebase_auth/lib/firebase_auth.dart | 47 ++++++++------ .../test/firebase_auth_test.dart | 60 ++++++++++++------ 6 files changed, 115 insertions(+), 83 deletions(-) diff --git a/packages/firebase_auth/CHANGELOG.md b/packages/firebase_auth/CHANGELOG.md index b4f905b06d1e..661dad2eaf0e 100644 --- a/packages/firebase_auth/CHANGELOG.md +++ b/packages/firebase_auth/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.6.0 + +* Added support for `updatePassword` in `FirebaseUser`. +* **Breaking Change** Moved `updateEmail` and `updateProfile` to `FirebaseUser`. + This brings the `firebase_auth` package inline with other implementations and documentation. + ## 0.5.20 * Replaced usages of guava's: ImmutableList and ImmutableMap with platform @@ -15,7 +21,7 @@ Collections.unmodifiableList() and Collections.unmodifiableMap(). ## 0.5.17 -* Adding support for FirebaseUser.delete. +* Adding support for FirebaseUser.delete. ## 0.5.16 diff --git a/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java b/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java index 87f5c552f1f8..6cd765ca5453 100755 --- a/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java +++ b/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java @@ -111,12 +111,15 @@ public void onMethodCall(MethodCall call, Result result) { case "linkWithFacebookCredential": handleLinkWithFacebookCredential(call, result); break; - case "updateProfile": - handleUpdateProfile(call, result); - break; case "updateEmail": handleUpdateEmail(call, result); break; + case "updatePassword": + handleUpdatePassword(call, result); + break; + case "updateProfile": + handleUpdateProfile(call, result); + break; case "startListeningAuthState": handleStartListeningAuthState(call, result); break; @@ -139,6 +142,7 @@ public void onMethodCall(MethodCall call, Result result) { } private void handleSignInWithPhoneNumber(MethodCall call, Result result) { + @SuppressWarnings("unchecked") Map arguments = (Map) call.arguments; String verificationId = arguments.get("verificationId"); String smsCode = arguments.get("smsCode"); @@ -424,6 +428,24 @@ public void onComplete(@NonNull Task task) { }); } + private void handleUpdateEmail(MethodCall call, final Result result) { + @SuppressWarnings("unchecked") + Map arguments = (Map) call.arguments; + firebaseAuth + .getCurrentUser() + .updateEmail(arguments.get("email")) + .addOnCompleteListener(new TaskVoidCompleteListener(result)); + } + + private void handleUpdatePassword(MethodCall call, final Result result) { + @SuppressWarnings("unchecked") + Map arguments = (Map) call.arguments; + firebaseAuth + .getCurrentUser() + .updatePassword(arguments.get("password")) + .addOnCompleteListener(new TaskVoidCompleteListener(result)); + } + private void handleUpdateProfile(MethodCall call, final Result result) { @SuppressWarnings("unchecked") Map arguments = (Map) call.arguments; @@ -439,40 +461,7 @@ private void handleUpdateProfile(MethodCall call, final Result result) { firebaseAuth .getCurrentUser() .updateProfile(builder.build()) - .addOnCompleteListener( - new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (!task.isSuccessful()) { - Exception e = task.getException(); - result.error(ERROR_REASON_EXCEPTION, e.getMessage(), null); - } else { - result.success(null); - } - } - }); - } - - private void handleUpdateEmail(MethodCall call, final Result result) { - @SuppressWarnings("unchecked") - Map arguments = (Map) call.arguments; - String email = arguments.get("email"); - - firebaseAuth - .getCurrentUser() - .updateEmail(email) - .addOnCompleteListener( - new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (!task.isSuccessful()) { - Exception e = task.getException(); - result.error(ERROR_REASON_EXCEPTION, e.getMessage(), null); - } else { - result.success(null); - } - } - }); + .addOnCompleteListener(new TaskVoidCompleteListener(result)); } private void handleStartListeningAuthState(MethodCall call, final Result result) { diff --git a/packages/firebase_auth/example/lib/main.dart b/packages/firebase_auth/example/lib/main.dart index 4268083c7687..34ad301ca801 100755 --- a/packages/firebase_auth/example/lib/main.dart +++ b/packages/firebase_auth/example/lib/main.dart @@ -99,7 +99,7 @@ class _MyHomePageState extends State { (AuthException authException) { setState(() { _message = Future.value( - 'Phone numbber verification failed. Code: ${authException.code}. Message: ${authException.message}'); + 'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}'); }); }; diff --git a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m index a1452990d08f..ed2aa7126d4e 100644 --- a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m +++ b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m @@ -176,6 +176,18 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result completion:^(FIRUser *user, NSError *error) { [self sendResult:result forUser:user error:error]; }]; + } else if ([@"updateEmail" isEqualToString:call.method]) { + NSString *email = call.arguments[@"email"]; + [[FIRAuth auth].currentUser updateEmail:email + completion:^(NSError *error) { + [self sendResult:result forUser:nil error:error]; + }]; + } else if ([@"updatePassword" isEqualToString:call.method]) { + NSString *password = call.arguments[@"password"]; + [[FIRAuth auth].currentUser updatePassword:password + completion:^(NSError *error) { + [self sendResult:result forUser:nil error:error]; + }]; } else if ([@"updateProfile" isEqualToString:call.method]) { FIRUserProfileChangeRequest *changeRequest = [[FIRAuth auth].currentUser profileChangeRequest]; if (call.arguments[@"displayName"]) { @@ -187,12 +199,6 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result [changeRequest commitChangesWithCompletion:^(NSError *error) { [self sendResult:result forUser:nil error:error]; }]; - } else if ([@"updateEmail" isEqualToString:call.method]) { - NSString *toEmail = call.arguments[@"email"]; - [[FIRAuth auth].currentUser updateEmail:toEmail - completion:^(NSError *_Nullable error) { - [self sendResult:result forUser:nil error:error]; - }]; } else if ([@"signInWithCustomToken" isEqualToString:call.method]) { NSString *token = call.arguments[@"token"]; [[FIRAuth auth] signInWithCustomToken:token diff --git a/packages/firebase_auth/lib/firebase_auth.dart b/packages/firebase_auth/lib/firebase_auth.dart index 5574bc5ee1b9..fdbdffe448b4 100755 --- a/packages/firebase_auth/lib/firebase_auth.dart +++ b/packages/firebase_auth/lib/firebase_auth.dart @@ -95,6 +95,33 @@ class FirebaseUser extends UserInfo { await FirebaseAuth.channel.invokeMethod('delete'); } + /// Updates the email address of the user. + Future updateEmail(String email) async { + assert(email != null); + return await FirebaseAuth.channel.invokeMethod( + 'updateEmail', + {'email': email}, + ); + } + + /// Updates the password of the user. + Future updatePassword(String password) async { + assert(password != null); + return await FirebaseAuth.channel.invokeMethod( + 'updatePassword', + {'password': password}, + ); + } + + /// Updates the user profile information. + Future updateProfile(UserUpdateInfo userUpdateInfo) async { + assert(userUpdateInfo != null); + return await FirebaseAuth.channel.invokeMethod( + 'updateProfile', + userUpdateInfo._updateData, + ); + } + @override String toString() { return '$runtimeType($_data)'; @@ -369,26 +396,6 @@ class FirebaseAuth { return currentUser; } - Future updateEmail({ - @required String email, - }) async { - assert(email != null); - return await channel.invokeMethod( - 'updateEmail', - { - 'email': email, - }, - ); - } - - Future updateProfile(UserUpdateInfo userUpdateInfo) async { - assert(userUpdateInfo != null); - return await channel.invokeMethod( - 'updateProfile', - userUpdateInfo._updateData, - ); - } - /// Links google account with current user and returns [Future] /// /// throws [PlatformException] when diff --git a/packages/firebase_auth/test/firebase_auth_test.dart b/packages/firebase_auth/test/firebase_auth_test.dart index 95bb3f23c525..979f15a7750e 100755 --- a/packages/firebase_auth/test/firebase_auth_test.dart +++ b/packages/firebase_auth/test/firebase_auth_test.dart @@ -41,10 +41,9 @@ void main() { return mockHandleId++; break; case "sendPasswordResetEmail": - case "updateProfile": - return null; - break; case "updateEmail": + case "updatePassword": + case "updateProfile": return null; break; case "fetchProvidersForEmail": @@ -359,13 +358,52 @@ void main() { ); }); + test('updateEmail', () async { + final FirebaseUser user = await auth.currentUser(); + await user.updateEmail(kMockEmail); + expect(log, [ + isMethodCall( + 'currentUser', + arguments: null, + ), + isMethodCall( + 'updateEmail', + arguments: { + 'email': kMockEmail, + }, + ), + ]); + }); + + test('updatePassword', () async { + final FirebaseUser user = await auth.currentUser(); + await user.updatePassword(kMockPassword); + expect(log, [ + isMethodCall( + 'currentUser', + arguments: null, + ), + isMethodCall( + 'updatePassword', + arguments: { + 'password': kMockPassword, + }, + ), + ]); + }); + test('updateProfile', () async { final UserUpdateInfo userUpdateInfo = new UserUpdateInfo(); userUpdateInfo.photoUrl = kMockPhotoUrl; userUpdateInfo.displayName = kMockDisplayName; - await auth.updateProfile(userUpdateInfo); + final FirebaseUser user = await auth.currentUser(); + await user.updateProfile(userUpdateInfo); expect(log, [ + isMethodCall( + 'currentUser', + arguments: null, + ), isMethodCall( 'updateProfile', arguments: { @@ -376,20 +414,6 @@ void main() { ]); }); - test('updateEmail', () async { - final String updatedEmail = 'atestemail@gmail.com'; - auth.updateEmail(email: updatedEmail); - expect( - log, - [ - isMethodCall( - 'updateEmail', - arguments: {'email': updatedEmail}, - ), - ], - ); - }); - test('signInWithCustomToken', () async { final FirebaseUser user = await auth.signInWithCustomToken(token: kMockCustomToken);