From f9e277f5c7ed5411c1034abdc79fc9dc0cb37c1c Mon Sep 17 00:00:00 2001 From: "WINDOWS-JC\\John Castro" Date: Mon, 19 Jun 2017 14:06:34 -0600 Subject: [PATCH] Firebase Phone Auth Android IOS no test --- README.md | 11 +++-- package.json | 4 +- src/android/FirebasePlugin.java | 77 +++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 88c37e91f..02ead7a6e 100644 --- a/README.md +++ b/README.md @@ -331,12 +331,15 @@ window.FirebasePlugin.setDefaults(defaults); window.FirebasePlugin.setDefaults(defaults, "namespace"); ``` -### Phone Authetication (iOS only) +### Phone Authetication -IMPORTANT: SETUP YOUR PUSH NOTIFICATIONS FIRST, AND VERIFY THAT THEY ARE ARRIVING TO YOUR PHYSICAL DEVICE BEFORE YOU TEST THIS METHOD. USE THE APNS AUTH KEY TO GENERATE THE .P8 FILE AND UPLOAD IT TO FIREBASE. -WHEN YOU CALL THIS METHOD, FCM SENDS A SILENT PUSH TO THE DEVICE TO VERIFY IT. -This method sends an SMS to the user with the SMS_code and gets the verification id you need to continue the sign in process, with the Firebase JS SDK. +(iOS){ + IMPORTANT: SETUP YOUR PUSH NOTIFICATIONS FIRST, AND VERIFY THAT THEY ARE ARRIVING TO YOUR PHYSICAL DEVICE BEFORE YOU TEST THIS METHOD. USE THE APNS AUTH KEY TO GENERATE THE .P8 FILE AND UPLOAD IT TO FIREBASE. + WHEN YOU CALL THIS METHOD, FCM SENDS A SILENT PUSH TO THE DEVICE TO VERIFY IT. + + This method sends an SMS to the user with the SMS_code and gets the verification id you need to continue the sign in process, with the Firebase JS SDK. +} ``` window.FirebasePlugin.getVerificationID("+573123456789",function(id) { diff --git a/package.json b/package.json index c1f1a3c66..b843e1783 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "author": { - "name": "Robert Arnesson, Juan Semaan" + "name": "Robert Arnesson, Juan Semaan, John Castro, " }, "license": "MIT", "repository": { "type": "git", - "url": "https://github.com/silverio/cordova-plugin-firebase" + "url": "https://github.com/jestcastro/cordova-plugin-firebase" }, "name": "cordova-plugin-firebase", "version": "0.1.21", diff --git a/src/android/FirebasePlugin.java b/src/android/FirebasePlugin.java index c986ec6fe..6a227dba5 100755 --- a/src/android/FirebasePlugin.java +++ b/src/android/FirebasePlugin.java @@ -129,6 +129,9 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } else if (action.equals("setConfigSettings")) { this.setConfigSettings(callbackContext, args.getJSONObject(0)); return true; + }else if (action.equals("getVerificationID")) { + this.getVerificationID(callbackContext, args.getJSONObject(0)); + return true; } else if (action.equals("setDefaults")) { if (args.length() > 1) this.setDefaults(callbackContext, args.getJSONObject(0), args.getString(1)); else this.setDefaults(callbackContext, args.getJSONObject(0), null); @@ -577,4 +580,78 @@ private static Map defaultsToMap(JSONObject object) throws JSONE } return map; } + public void getVerificationID(final CallbackContext callbackContext, final String number) { + cordova.getThreadPool().execute(new Runnable() { + public void run() { + try { + mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { + @Override + public void onVerificationCompleted(PhoneAuthCredential credential) { + // This callback will be invoked in two situations: + // 1 - Instant verification. In some cases the phone number can be instantly + // verified without needing to send or enter a verification code. + // 2 - Auto-retrieval. On some devices Google Play services can automatically + // detect the incoming verification SMS and perform verificaiton without + // user action. + Log.d(TAG, "success: verifyPhoneNumber.onVerificationCompleted - doing nothing. sign in with token from onCodeSent"); + + // does this fire in cordova? + // TODO: return credential + } + + @Override + public void onVerificationFailed(FirebaseException e) { + // This callback is invoked in an invalid request for verification is made, + // for instance if the the phone number format is not valid. + Log.w(TAG, "failed: verifyPhoneNumber.onVerificationFailed ", e); + + String errorMsg = "unknown error verifying number"; + if (e instanceof FirebaseAuthInvalidCredentialsException) { + // The phone number is invalid + errorMsg = "Invalid phone number"; + } else if (e instanceof FirebaseTooManyRequestsException) { + errorMsg = "The SMS quota for the project has been exceeded"; + } + + callbackContext.error(errorMsg); + } + + @Override + public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken token) { + // The SMS verification code has been sent to the provided phone number, we + // now need to ask the user to enter the code and then construct a credential + // by combining the code with a verification ID [(in app)]. + Log.d(TAG, "success: verifyPhoneNumber.onCodeSent"); + + JSONObject returnResults = new JSONObject(); + try { + returnResults.put("verificationId", verificationId); + //returnResults.put("forceResendingToken", token); // TODO: return forceResendingToken + } catch (JSONException e) { + callbackContext.error(e.getMessage()); + return; + } + PluginResult pluginresult = new PluginResult(PluginResult.Status.OK, returnResults); + pluginresult.setKeepCallback(true); + callbackContext.sendPluginResult(pluginresult); + } + }; + + PhoneAuthProvider.getInstance().verifyPhoneNumber( + number, // Phone number to verify + 60, // Timeout duration + TimeUnit.SECONDS, // Unit of timeout + cordova.getActivity(), // Activity (for callback binding) + mCallbacks); // OnVerificationStateChangedCallbacks + //resentToken); // The ForceResendingToken obtained from onCodeSent callback + // to force re-sending another verification SMS before the auto-retrieval timeout. + // TODO: make resendToken accessible + + + } catch (Exception e) { + callbackContext.error(e.getMessage()); + } + } + }); + } }