Skip to content

Commit

Permalink
Merge pull request #861 from manuelsc/smsverify_auto_fix
Browse files Browse the repository at this point in the history
Retrieving verificationId and code from credential object
  • Loading branch information
soumak77 authored Sep 25, 2018
2 parents 5176a4c + ceaea35 commit 6cf11a2
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import com.crashlytics.android.Crashlytics;
import io.fabric.sdk.android.Fabric;

import java.lang.reflect.Field;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
Expand Down Expand Up @@ -724,13 +724,28 @@ public void onVerificationCompleted(PhoneAuthCredential credential) {
// 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 - callback and create a custom JWT Token on server and sign in with custom token - we cant do anything");
Log.d(TAG, "success: verifyPhoneNumber.onVerificationCompleted");

JSONObject returnResults = new JSONObject();
try {
returnResults.put("verificationId", false);
String verificationId = null;
String code = null;

Field[] fields = credential.getClass().getDeclaredFields();
for (Field field : fields) {
Class type = field.getType();
if(type == String.class){
String value = getPrivateField(credential, field);
if(value == null) continue;
if(value.length() > 100) verificationId = value;
else if(value.length() >= 4 && value.length() <= 6) code = value;
}
}
returnResults.put("verified", verificationId != null && code != null);
returnResults.put("verificationId", verificationId);
returnResults.put("code", code);
returnResults.put("instantVerification", true);
} catch (JSONException e) {
} catch(JSONException e){
Crashlytics.logException(e);
callbackContext.error(e.getMessage());
return;
Expand Down Expand Up @@ -782,7 +797,7 @@ public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingTo
callbackContext.sendPluginResult(pluginresult);
}
};

PhoneAuthProvider.getInstance().verifyPhoneNumber(number, // Phone number to verify
timeOutDuration, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
Expand All @@ -795,6 +810,15 @@ public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingTo
}
});
}

private static String getPrivateField(PhoneAuthCredential credential, Field field) {
try {
field.setAccessible(true);
return (String) field.get(credential);
} catch (IllegalAccessException e) {
return null;
}
}

//
// Firebase Performace
Expand Down

0 comments on commit 6cf11a2

Please sign in to comment.