From cfb8548d747bd1791b0476a48b7adacff257c92f Mon Sep 17 00:00:00 2001 From: Dave Alden Date: Thu, 24 Oct 2019 15:03:22 +0100 Subject: [PATCH] [Android] Add direct support to sign in and link with credentials obtained via verifyPhoneNumber() --- src/android/FirebasePlugin.java | 79 ++++++++++++++++++++++++++++++++- www/firebase.js | 33 ++++++++------ 2 files changed, 98 insertions(+), 14 deletions(-) diff --git a/src/android/FirebasePlugin.java b/src/android/FirebasePlugin.java index a9c42c4ab..6207bdfd7 100755 --- a/src/android/FirebasePlugin.java +++ b/src/android/FirebasePlugin.java @@ -9,21 +9,27 @@ import android.media.AudioAttributes; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; + +import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; + import android.util.Base64; import android.util.Log; import com.crashlytics.android.Crashlytics; import java.lang.reflect.Field; + +import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.Task; import com.google.firebase.FirebaseApp; import com.google.firebase.analytics.FirebaseAnalytics; +import com.google.firebase.auth.AuthResult; +import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.remoteconfig.FirebaseRemoteConfig; @@ -191,6 +197,12 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } else if (action.equals("verifyPhoneNumber")) { this.verifyPhoneNumber(callbackContext, args.getString(0), args.getInt(1)); return true; + } else if (action.equals("signInWithCredential")) { + this.signInWithCredential(callbackContext, args); + return true; + } else if (action.equals("linkUserWithCredential")) { + this.linkUserWithCredential(callbackContext, args); + return true; } else if (action.equals("startTrace")) { this.startTrace(callbackContext, args.getString(0)); return true; @@ -889,6 +901,71 @@ private static String getPrivateField(PhoneAuthCredential credential, Field fiel } } + public void signInWithCredential(final CallbackContext callbackContext, JSONArray args){ + cordova.getThreadPool().execute(new Runnable() { + public void run() { + try { + String verificationId = args.getString(0); + String code = args.getString(1); + PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); + + FirebaseAuth.getInstance().signInWithCredential(credential).addOnCompleteListener(cordova.getActivity(), new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + PluginResult pluginresult = new PluginResult(PluginResult.Status.OK); + if (!task.isSuccessful()) { + String errMessage; + if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { + errMessage = "Invalid verification code"; + }else{ + errMessage = task.getException().toString(); + } + pluginresult = new PluginResult(PluginResult.Status.ERROR, errMessage); + } + callbackContext.sendPluginResult(pluginresult); + } + } + ); + } catch (Exception e) { + handleExceptionWithContext(e, callbackContext); + } + } + }); + } + + public void linkUserWithCredential(final CallbackContext callbackContext, JSONArray args){ + cordova.getThreadPool().execute(new Runnable() { + public void run() { + try { + String verificationId = args.getString(0); + String code = args.getString(1); + PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); + + FirebaseAuth.getInstance().getCurrentUser().linkWithCredential(credential).addOnCompleteListener(cordova.getActivity(), new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + PluginResult pluginresult = new PluginResult(PluginResult.Status.OK); + if (!task.isSuccessful()) { + String errMessage; + if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { + errMessage = "Invalid verification code"; + }else{ + errMessage = task.getException().toString(); + } + pluginresult = new PluginResult(PluginResult.Status.ERROR, errMessage); + } + callbackContext.sendPluginResult(pluginresult); + } + } + ); + } catch (Exception e) { + handleExceptionWithContext(e, callbackContext); + } + } + }); + } + + // // Firebase Performace // diff --git a/www/firebase.js b/www/firebase.js index dce9408df..d0b5921ad 100644 --- a/www/firebase.js +++ b/www/firebase.js @@ -152,19 +152,6 @@ exports.setPerformanceCollectionEnabled = function (enabled, success, error) { }; - -exports.verifyPhoneNumber = function (number, timeOutDuration, success, error) { - if (typeof timeOutDuration === 'function') { - // method being called with old signature: function(number, success, error) - // timeOutDuration is the success callback, success is the error callback - exec(timeOutDuration, success, "FirebasePlugin", "verifyPhoneNumber", [number]); - } else { - // method being called with new signature: function(number, timeOutDuration, success, error) - // callbacks are correctly named - exec(success, error, "FirebasePlugin", "verifyPhoneNumber", [number, timeOutDuration]); - } -}; - exports.clearAllNotifications = function (success, error) { exec(success, error, "FirebasePlugin", "clearAllNotifications", []); }; @@ -202,3 +189,23 @@ exports.setCrashlyticsUserId = function (userId, success, error) { }; +// Authentication +exports.verifyPhoneNumber = function (number, timeOutDuration, success, error) { + if (typeof timeOutDuration === 'function') { + // method being called with old signature: function(number, success, error) + // timeOutDuration is the success callback, success is the error callback + exec(timeOutDuration, success, "FirebasePlugin", "verifyPhoneNumber", [number]); + } else { + // method being called with new signature: function(number, timeOutDuration, success, error) + // callbacks are correctly named + exec(success, error, "FirebasePlugin", "verifyPhoneNumber", [number, timeOutDuration]); + } +}; + +exports.signInWithCredential = function (verificationId, code, success, error) { + exec(success, error, "FirebasePlugin", "signInWithCredential", [verificationId, code]); +}; + +exports.linkUserWithCredential = function (verificationId, code, success, error) { + exec(success, error, "FirebasePlugin", "linkUserWithCredential", [verificationId, code]); +};