Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public void onMethodCall(MethodCall call, Result result) {
case "updateEmail":
handleUpdateEmail(call, result);
break;
case "updatePassword":
handleUpdatePassword(call, result);
break;
case "updatePhoneNumber":
handleUpdatePhoneNumber(call, result);
break;
case "startListeningAuthState":
handleStartListeningAuthState(call, result);
break;
Expand Down Expand Up @@ -155,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<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Map<String, Object> arguments = new HashMap<>();
arguments.put("handle", handle);
channel.invokeMethod("phoneVerificationCompleted", arguments);
if (loginAfter) {
firebaseAuth
.signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(
new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Map<String, Object> arguments = new HashMap<>();
arguments.put("handle", handle);
channel.invokeMethod("phoneVerificationCompleted", arguments);
}
}
}
});
});
} else {
Map<String, Object> arguments = new HashMap<>();
arguments.put("handle", handle);
channel.invokeMethod("phoneVerificationCompleted", arguments);
}
}

@Override
Expand Down Expand Up @@ -475,6 +488,54 @@ public void onComplete(@NonNull Task<Void> task) {
});
}

private void handleUpdatePassword(MethodCall call, final Result result) {
@SuppressWarnings("unchecked")
Map<String, String> arguments = (Map<String, String>) call.arguments;
String password = arguments.get("password");

firebaseAuth
.getCurrentUser()
.updatePassword(password)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Exception e = task.getException();
result.error(ERROR_REASON_EXCEPTION, e.getMessage(), null);
} else {
result.success(null);
}
}
});
}

private void handleUpdatePhoneNumber(MethodCall call, final Result result) {
@SuppressWarnings("unchecked")
Map<String, String> arguments = (Map<String, String>) 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<Void>() {
@Override
public void onComplete(@NonNull Task<Void> 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 =
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_auth/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class _MyHomePageState extends State<MyHomePage> {
(AuthException authException) {
setState(() {
_message = Future<String>.value(
'Phone numbber verification failed. Code: ${authException.code}. Message: ${authException.message}');
'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
});
};

Expand Down
18 changes: 18 additions & 0 deletions packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ - (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 ([@"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
Expand Down
41 changes: 32 additions & 9 deletions packages/firebase_auth/lib/firebase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,15 @@ class FirebaseAuth {
return currentUser;
}

Future<void> verifyPhoneNumber({
@required String phoneNumber,
@required Duration timeout,
int forceResendingToken,
@required PhoneVerificationCompleted verificationCompleted,
@required PhoneVerificationFailed verificationFailed,
@required PhoneCodeSent codeSent,
@required PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout,
}) async {
Future<void> 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<String, dynamic> callbacks = <String, dynamic>{
'PhoneVerificationCompleted': verificationCompleted,
'PhoneVerificationFailed': verificationFailed,
Expand All @@ -316,6 +316,7 @@ class FirebaseAuth {
'phoneNumber': phoneNumber,
'timeout': timeout.inMilliseconds,
'forceResendingToken': forceResendingToken,
'loginAfter': loginAfter
};

await channel.invokeMethod('verifyPhoneNumber', params);
Expand Down Expand Up @@ -381,6 +382,28 @@ class FirebaseAuth {
);
}

Future<void> updatePassword({
@required String password,
}) async {
assert(password != null);
return await channel.invokeMethod(
'updatePassword',
<String, String>{
'password': password,
},
);
}

Future<void> updatePhoneNumber(
{@required String verificationId, @required String smsCode}) async {
assert(verificationId != null);
assert(smsCode != null);
return await channel.invokeMethod(
'updatePhoneNumber',
<String, String>{'verificationId': verificationId, 'smsCode': smsCode},
);
}

Future<void> updateProfile(UserUpdateInfo userUpdateInfo) async {
assert(userUpdateInfo != null);
return await channel.invokeMethod(
Expand Down
34 changes: 34 additions & 0 deletions packages/firebase_auth/test/firebase_auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ void main() {
return null;
break;
case "updateEmail":
case "updatePassword":
case "updatePhoneNumber":
return null;
break;
case "fetchProvidersForEmail":
Expand Down Expand Up @@ -390,6 +392,38 @@ void main() {
);
});

test('updatePassword', () async {
final String updatedPassword = 'password123';
auth.updatePassword(password: updatedPassword);
expect(
log,
<Matcher>[
isMethodCall(
'updatePassword',
arguments: <String, String>{'password': updatedPassword},
),
],
);
});

test('updatePhoneNumber', () async {
final String verificationId = 'thisIsAVerificationCode';
final String smsCode = '123456';
auth.updatePhoneNumber(verificationId: verificationId, smsCode: smsCode);
expect(
log,
<Matcher>[
isMethodCall(
'updatePhoneNumber',
arguments: <String, String>{
'verificationId': verificationId,
'smsCode': smsCode
},
),
],
);
});

test('signInWithCustomToken', () async {
final FirebaseUser user =
await auth.signInWithCustomToken(token: kMockCustomToken);
Expand Down