From 418198a0a803eb3896c4e0022f5f901a5d3f7ff2 Mon Sep 17 00:00:00 2001 From: Kirollos Morkos Date: Tue, 7 Aug 2018 17:52:31 -0400 Subject: [PATCH 1/3] Add updatePassword method --- .../firebaseauth/FirebaseAuthPlugin.java | 25 +++++++++++++++++++ .../ios/Classes/FirebaseAuthPlugin.m | 6 +++++ packages/firebase_auth/lib/firebase_auth.dart | 12 +++++++++ .../test/firebase_auth_test.dart | 15 +++++++++++ 4 files changed, 58 insertions(+) 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 d75e8be0dd19..383ae73f3d47 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 @@ -117,6 +117,9 @@ public void onMethodCall(MethodCall call, Result result) { case "updateEmail": handleUpdateEmail(call, result); break; + case "updatePassword": + handleupdatePassword(call, result); + break; case "startListeningAuthState": handleStartListeningAuthState(call, result); break; @@ -475,6 +478,28 @@ public void onComplete(@NonNull Task task) { }); } + private void handleUpdatePassword(MethodCall call, final Result result) { + @SuppressWarnings("unchecked") + Map arguments = (Map) call.arguments; + String password = arguments.get("password"); + + firebaseAuth + .getCurrentUser() + .updatePassword(password) + .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 handleStartListeningAuthState(MethodCall call, final Result result) { final int handle = nextHandle++; FirebaseAuth.AuthStateListener listener = diff --git a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m index 548e8ace0caa..353fdb26329a 100644 --- a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m +++ b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m @@ -193,6 +193,12 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result completion:^(NSError *_Nullable error) { [self sendResult:result forUser:nil error:error]; }]; + } else if ([@"updatePassword" isEqualToString:call.method]) { + NSString *toPassword = call.arguments[@"password"]; + [[FIRAuth auth].currentUser updatePassword:toPassword + 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..fbecc924ac74 100755 --- a/packages/firebase_auth/lib/firebase_auth.dart +++ b/packages/firebase_auth/lib/firebase_auth.dart @@ -381,6 +381,18 @@ class FirebaseAuth { ); } + Future updatePassword({ + @required String password, + }) async { + assert(password != null); + return await channel.invokeMethod( + 'updatePassword', + { + 'password': password, + }, + ); + } + Future updateProfile(UserUpdateInfo userUpdateInfo) async { assert(userUpdateInfo != null); return await channel.invokeMethod( diff --git a/packages/firebase_auth/test/firebase_auth_test.dart b/packages/firebase_auth/test/firebase_auth_test.dart index 95bb3f23c525..ba9ba5d7d8c3 100755 --- a/packages/firebase_auth/test/firebase_auth_test.dart +++ b/packages/firebase_auth/test/firebase_auth_test.dart @@ -45,6 +45,7 @@ void main() { return null; break; case "updateEmail": + case "updatePassword": return null; break; case "fetchProvidersForEmail": @@ -390,6 +391,20 @@ void main() { ); }); + test('updatePassword', () async { + final String updatedPassword = 'password123'; + auth.updatePassword(password: updatedPassword); + expect( + log, + [ + isMethodCall( + 'updatePassword', + arguments: {'password': updatedPassword}, + ), + ], + ); + }); + test('signInWithCustomToken', () async { final FirebaseUser user = await auth.signInWithCustomToken(token: kMockCustomToken); From ce221b43d2de9a80ed5297d5c007ed58fa9eaf8c Mon Sep 17 00:00:00 2001 From: Kirollos Morkos Date: Tue, 7 Aug 2018 20:45:07 -0400 Subject: [PATCH 2/3] Add updatePhoneNumber method --- .../firebaseauth/FirebaseAuthPlugin.java | 62 +++++++++++++++---- packages/firebase_auth/example/lib/main.dart | 2 +- .../ios/Classes/FirebaseAuthPlugin.m | 12 ++++ packages/firebase_auth/lib/firebase_auth.dart | 17 +++++ .../test/firebase_auth_test.dart | 16 +++++ 5 files changed, 95 insertions(+), 14 deletions(-) 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 383ae73f3d47..caa8572b255d 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 @@ -118,7 +118,10 @@ public void onMethodCall(MethodCall call, Result result) { handleUpdateEmail(call, result); break; case "updatePassword": - handleupdatePassword(call, result); + handleUpdatePassword(call, result); + break; + case "updatePhoneNumber": + handleUpdatePhoneNumber(call, result); break; case "startListeningAuthState": handleStartListeningAuthState(call, result); @@ -158,24 +161,31 @@ private void handleVerifyPhoneNumber(MethodCall call, Result result) { final int handle = call.argument("handle"); String phoneNumber = call.argument("phoneNumber"); int timeout = call.argument("timeout"); + final boolean loginAfter = call.argument("loginAfter"); PhoneAuthProvider.OnVerificationStateChangedCallbacks verificationCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) { - firebaseAuth - .signInWithCredential(phoneAuthCredential) - .addOnCompleteListener( - new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (task.isSuccessful()) { - Map arguments = new HashMap<>(); - arguments.put("handle", handle); - channel.invokeMethod("phoneVerificationCompleted", arguments); + if (loginAfter) { + firebaseAuth + .signInWithCredential(phoneAuthCredential) + .addOnCompleteListener( + new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + Map arguments = new HashMap<>(); + arguments.put("handle", handle); + channel.invokeMethod("phoneVerificationCompleted", arguments); + } } - } - }); + }); + } else { + Map arguments = new HashMap<>(); + arguments.put("handle", handle); + channel.invokeMethod("phoneVerificationCompleted", arguments); + } } @Override @@ -500,6 +510,32 @@ public void onComplete(@NonNull Task task) { }); } + private void handleUpdatePhoneNumber(MethodCall call, final Result result) { + @SuppressWarnings("unchecked") + Map arguments = (Map) call.arguments; + String verificationId = arguments.get("verificationId"); + String smsCode = arguments.get("smsCode"); + + PhoneAuthCredential phoneAuthCredential = + PhoneAuthProvider.getCredential(verificationId, smsCode); + + firebaseAuth + .getCurrentUser() + .updatePhoneNumber(phoneAuthCredential) + .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 handleStartListeningAuthState(MethodCall call, final Result result) { final int handle = nextHandle++; FirebaseAuth.AuthStateListener listener = 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 353fdb26329a..3a8319477308 100644 --- a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m +++ b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m @@ -199,6 +199,18 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result completion:^(NSError *_Nullable error) { [self sendResult:result forUser:nil error:error]; }]; + } else if ([@"updatePhoneNumber" isEqualToString:call.method]) { + NSString *verificationId = call.arguments[@"verificationId"]; + NSString *smsCode = call.arguments[@"smsCode"]; + + FIRPhoneAuthCredential *credential = + [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationId + verificationCode:smsCode]; + [[FIRAuth auth].currentUser updatePhoneNumber:credential + completion:^(NSError *_Nullable error) { + [self sendResult:result forUser:nil error:error]; + }]; + NSString *toPassword = call.arguments[@"password"]; } 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 fbecc924ac74..b56d6d54147c 100755 --- a/packages/firebase_auth/lib/firebase_auth.dart +++ b/packages/firebase_auth/lib/firebase_auth.dart @@ -301,6 +301,7 @@ class FirebaseAuth { @required PhoneVerificationFailed verificationFailed, @required PhoneCodeSent codeSent, @required PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout, + bool loginAfter = true }) async { final Map callbacks = { 'PhoneVerificationCompleted': verificationCompleted, @@ -316,6 +317,7 @@ class FirebaseAuth { 'phoneNumber': phoneNumber, 'timeout': timeout.inMilliseconds, 'forceResendingToken': forceResendingToken, + 'loginAfter': loginAfter }; await channel.invokeMethod('verifyPhoneNumber', params); @@ -393,6 +395,21 @@ class FirebaseAuth { ); } + Future updatePhoneNumber({ + @required String verificationId, + @required String smsCode + }) async { + assert(verificationId != null); + assert(smsCode != null); + return await channel.invokeMethod( + 'updatePhoneNumber', + { + 'verificationId': verificationId, + 'smsCode': smsCode + }, + ); + } + Future updateProfile(UserUpdateInfo userUpdateInfo) async { assert(userUpdateInfo != null); return await channel.invokeMethod( diff --git a/packages/firebase_auth/test/firebase_auth_test.dart b/packages/firebase_auth/test/firebase_auth_test.dart index ba9ba5d7d8c3..0d3f4c24fbc4 100755 --- a/packages/firebase_auth/test/firebase_auth_test.dart +++ b/packages/firebase_auth/test/firebase_auth_test.dart @@ -46,6 +46,7 @@ void main() { break; case "updateEmail": case "updatePassword": + case "updatePhoneNumber": return null; break; case "fetchProvidersForEmail": @@ -405,6 +406,21 @@ void main() { ); }); + test('updatePhoneNumber', () async { + final String verificationId = 'thisIsAVerificationCode'; + final String smsCode = '123456'; + auth.updatePhoneNumber(verificationId: verificationId, smsCode: smsCode); + expect( + log, + [ + isMethodCall( + 'updatePhoneNumber', + arguments: {'verificationId': verificationId, 'smsCode': smsCode}, + ), + ], + ); + }); + test('signInWithCustomToken', () async { final FirebaseUser user = await auth.signInWithCustomToken(token: kMockCustomToken); From 0e1b5aa0319e3eab260c3078b00f44aaac9d81e2 Mon Sep 17 00:00:00 2001 From: Kirollos Morkos Date: Tue, 7 Aug 2018 20:56:31 -0400 Subject: [PATCH 3/3] Fix formatting --- .../ios/Classes/FirebaseAuthPlugin.m | 12 ++++---- packages/firebase_auth/lib/firebase_auth.dart | 30 ++++++++----------- .../test/firebase_auth_test.dart | 5 +++- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m index 3a8319477308..38d05664d582 100644 --- a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m +++ b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m @@ -196,9 +196,9 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result } else if ([@"updatePassword" isEqualToString:call.method]) { NSString *toPassword = call.arguments[@"password"]; [[FIRAuth auth].currentUser updatePassword:toPassword - completion:^(NSError *_Nullable error) { - [self sendResult:result forUser:nil error:error]; - }]; + completion:^(NSError *_Nullable error) { + [self sendResult:result forUser:nil error:error]; + }]; } else if ([@"updatePhoneNumber" isEqualToString:call.method]) { NSString *verificationId = call.arguments[@"verificationId"]; NSString *smsCode = call.arguments[@"smsCode"]; @@ -207,9 +207,9 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationId verificationCode:smsCode]; [[FIRAuth auth].currentUser updatePhoneNumber:credential - completion:^(NSError *_Nullable error) { - [self sendResult:result forUser:nil error:error]; - }]; + completion:^(NSError *_Nullable error) { + [self sendResult:result forUser:nil error:error]; + }]; NSString *toPassword = call.arguments[@"password"]; } else if ([@"signInWithCustomToken" isEqualToString:call.method]) { NSString *token = call.arguments[@"token"]; diff --git a/packages/firebase_auth/lib/firebase_auth.dart b/packages/firebase_auth/lib/firebase_auth.dart index b56d6d54147c..a8fbb16ecce3 100755 --- a/packages/firebase_auth/lib/firebase_auth.dart +++ b/packages/firebase_auth/lib/firebase_auth.dart @@ -293,16 +293,15 @@ class FirebaseAuth { return currentUser; } - Future verifyPhoneNumber({ - @required String phoneNumber, - @required Duration timeout, - int forceResendingToken, - @required PhoneVerificationCompleted verificationCompleted, - @required PhoneVerificationFailed verificationFailed, - @required PhoneCodeSent codeSent, - @required PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout, - bool loginAfter = true - }) async { + Future verifyPhoneNumber( + {@required String phoneNumber, + @required Duration timeout, + int forceResendingToken, + @required PhoneVerificationCompleted verificationCompleted, + @required PhoneVerificationFailed verificationFailed, + @required PhoneCodeSent codeSent, + @required PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout, + bool loginAfter = true}) async { final Map callbacks = { 'PhoneVerificationCompleted': verificationCompleted, 'PhoneVerificationFailed': verificationFailed, @@ -395,18 +394,13 @@ class FirebaseAuth { ); } - Future updatePhoneNumber({ - @required String verificationId, - @required String smsCode - }) async { + Future updatePhoneNumber( + {@required String verificationId, @required String smsCode}) async { assert(verificationId != null); assert(smsCode != null); return await channel.invokeMethod( 'updatePhoneNumber', - { - 'verificationId': verificationId, - 'smsCode': smsCode - }, + {'verificationId': verificationId, 'smsCode': smsCode}, ); } diff --git a/packages/firebase_auth/test/firebase_auth_test.dart b/packages/firebase_auth/test/firebase_auth_test.dart index 0d3f4c24fbc4..367827e3b062 100755 --- a/packages/firebase_auth/test/firebase_auth_test.dart +++ b/packages/firebase_auth/test/firebase_auth_test.dart @@ -415,7 +415,10 @@ void main() { [ isMethodCall( 'updatePhoneNumber', - arguments: {'verificationId': verificationId, 'smsCode': smsCode}, + arguments: { + 'verificationId': verificationId, + 'smsCode': smsCode + }, ), ], );