Skip to content

Commit

Permalink
Firebase Phone Auth Android IOS no test
Browse files Browse the repository at this point in the history
  • Loading branch information
WINDOWS-JC\John Castro committed Jun 19, 2017
1 parent f9b7e80 commit f9e277f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
77 changes: 77 additions & 0 deletions src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -577,4 +580,78 @@ private static Map<String, Object> 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());
}
}
});
}
}

0 comments on commit f9e277f

Please sign in to comment.