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 @@ -119,7 +119,7 @@ public void onMethodCall(MethodCall call, Result result) {
handleReauthenticateWithCredential(call, result, getAuth(call));
break;
case "linkWithCredential":
handleLinkWithEmailAndPassword(call, result, getAuth(call));
handleLinkWithCredential(call, result, getAuth(call));
break;
case "unlinkFromProvider":
handleUnlinkFromProvider(call, result, getAuth(call));
Expand Down Expand Up @@ -173,24 +173,44 @@ private void handleVerifyPhoneNumber(
final int handle = (int) arguments.get("handle");
String phoneNumber = (String) arguments.get("phoneNumber");
int timeout = (int) arguments.get("timeout");
final boolean link = (boolean) arguments.get("link");

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 (link) {
firebaseAuth
.getCurrentUser()
.linkWithCredential(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 {
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);
}
}
});
}
}

@Override
Expand Down Expand Up @@ -263,13 +283,9 @@ private Map<String, Object> getVerifyPhoneNumberExceptionMap(FirebaseException e
return exceptionMap;
}

private void handleLinkWithEmailAndPassword(
MethodCall call, Result result, FirebaseAuth firebaseAuth) {
Map<String, String> arguments = call.arguments();
String email = arguments.get("email");
String password = arguments.get("password");
private void handleLinkWithCredential(MethodCall call, Result result, FirebaseAuth firebaseAuth) {
AuthCredential credential = getCredential((Map<String, Object>) call.arguments());

AuthCredential credential = EmailAuthProvider.getCredential(email, password);
firebaseAuth
.getCurrentUser()
.linkWithCredential(credential)
Expand Down Expand Up @@ -382,6 +398,13 @@ private AuthCredential getCredential(Map<String, Object> arguments) {
credential = GithubAuthProvider.getCredential(token);
break;
}
case PhoneAuthProvider.PROVIDER_ID:
{
String verificationId = data.get("verificationId");
String smsCode = data.get("smsCode");
credential = PhoneAuthProvider.getCredential(verificationId, smsCode);
break;
}
default:
{
credential = null;
Expand Down
24 changes: 15 additions & 9 deletions packages/firebase_auth/lib/src/firebase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,20 @@ class FirebaseAuth {
/// [codeAutoRetrievalTimeout] Optional callback.
/// It will trigger when SMS auto-retrieval times out and provide a
/// [verificationId].
Future<void> verifyPhoneNumber({
@required String phoneNumber,
@required Duration timeout,
int forceResendingToken,
@required PhoneVerificationCompleted verificationCompleted,
@required PhoneVerificationFailed verificationFailed,
@required PhoneCodeSent codeSent,
@required PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout,
}) async {
///
/// [linkCredentials] Default false.
/// Set to true when phone number verification is used to link a phone number to an existing user who is
/// currently logged in

Future<void> verifyPhoneNumber(
{@required String phoneNumber,
@required Duration timeout,
int forceResendingToken,
@required PhoneVerificationCompleted verificationCompleted,
@required PhoneVerificationFailed verificationFailed,
@required PhoneCodeSent codeSent,
@required PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout,
bool linkCredentials = false}) async {
final Map<String, dynamic> callbacks = <String, dynamic>{
'PhoneVerificationCompleted': verificationCompleted,
'PhoneVerificationFailed': verificationFailed,
Expand All @@ -262,6 +267,7 @@ class FirebaseAuth {
final Map<String, dynamic> params = <String, dynamic>{
'handle': nextHandle,
'phoneNumber': phoneNumber,
'link': linkCredentials,
'timeout': timeout.inMilliseconds,
'forceResendingToken': forceResendingToken,
'app': app.name,
Expand Down
1 change: 1 addition & 0 deletions packages/firebase_auth/test/firebase_auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ void main() {
isMethodCall('verifyPhoneNumber', arguments: <String, dynamic>{
'handle': 1,
'phoneNumber': kMockPhoneNumber,
'link': false,
'timeout': 5000,
'forceResendingToken': null,
'app': auth.app.name,
Expand Down