Skip to content

Commit

Permalink
[auth][android] Cache auto-verified credential for use on the native …
Browse files Browse the repository at this point in the history
…side
  • Loading branch information
chrisbianca committed Oct 26, 2017
1 parent 44278f5 commit f467e2d
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
class RNFirebaseAuth extends ReactContextBaseJavaModule {
private static final String TAG = "RNFirebaseAuth";
private String mVerificationId;
private PhoneAuthCredential mCredential;
private ReactContext mReactContext;
private HashMap<String, FirebaseAuth.AuthStateListener> mAuthListeners = new HashMap<>();
private HashMap<String, FirebaseAuth.IdTokenListener> mIdTokenListeners = new HashMap<>();
Expand Down Expand Up @@ -739,10 +740,16 @@ public void verifyPhoneNumber(final String appName, final String phoneNumber, fi

Log.d(TAG, "verifyPhoneNumber:" + phoneNumber);

// Reset the credential
mCredential = null;

PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

@Override
public void onVerificationCompleted(final PhoneAuthCredential phoneAuthCredential) {
// Cache the credential to protect against null verificationId
mCredential = phoneAuthCredential;

Log.d(TAG, "verifyPhoneNumber:verification:onVerificationCompleted");
WritableMap state = Arguments.createMap();

Expand Down Expand Up @@ -1069,6 +1076,15 @@ private AuthCredential getCredentialForProvider(String provider, String authToke
case "github.com":
return GithubAuthProvider.getCredential(authToken);
case "phone":
// If the phone number is auto-verified quickly, then the verificationId can be null
// We cached the credential as part of the verifyPhoneNumber request to be re-used here
// if possible
if (authToken == null && mCredential != null) {
PhoneAuthCredential credential = mCredential;
// Reset the cached credential
mCredential = null;
return credential;
}
return PhoneAuthProvider.getCredential(authToken, authSecret);
case "password":
return EmailAuthProvider.getCredential(authToken, authSecret);
Expand Down

0 comments on commit f467e2d

Please sign in to comment.