chemerisuk / cordova-plugin-firebase-authentication

Cordova plugin for Firebase Authentication
MIT License
98 stars 60 forks source link

in Case of instant signin on how can we get back credential back to firebase javascript sdk from plugin #84

Closed sitaram-dev closed 4 years ago

sitaram-dev commented 4 years ago

Hi sir your plugin contribution is superb in developing firebase phone authentication.

Sir without instant signin it's easy to get back credential to JavaScript firebase sdk and signing in using code var myVerificationId;

/ cordova.plugins.firebase.auth.verifyPhoneNumber(number, 30000).then(function(verificationId) { // pass verificationId to signInWithVerificationId myVerificationId=verificationId

  });

var credential = firebase.auth.PhoneAuthProvider.credential(myVerificationId, passcodeFromUser); firebase.auth().signInWithCredential(credential);

but in case of instant signin this does not work as there takes place some internal signing in process . is there any way out to get the credential back to javascript sdk in case of instant sign in??

sitaram-dev commented 4 years ago

I solved it

Goto hybrid/platforms/android/src/by/chemerisuk/cordova/firebase/FirebaseAuthenticationPlugin.java

in function verifyPhoneNumber comment out signInWithPhoneCredential(credential) to stop internal android instant sign in ; and get the smsCode using smsCode=credential.getSmsCode(); and pass it back to javascript along with verification id using callbackContext.success(smsCode+"/"+verificationId);

note: use timer to execute this function [callbackContext.success(smsCode+"/"+verificationId);] little later so it gets the smsCode for sure otherwise you will get user to manually provide it.

@CordovaMethod private void verifyPhoneNumber(String phoneNumber, long timeoutMillis, final CallbackContext callbackContext) {

@Override public void onVerificationCompleted(PhoneAuthCredential credential) {

                   // signInWithPhoneCredential(credential);
                   smsCode=credential.getSmsCode();
                }

                @Override
                public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken 
                 forceResendingToken) {
                 //   callbackContext.success(smsCodeAndVerificationId);
                   // we are using "/" to distinguish smsCode and Verification Id this will help breaking 
                    //two in javascript you can use your own logic to do the same.
                   // callbackContext.success(smsCode+"/"+verificationId);

              // other code

}

in javascript side

cordova.plugins.firebase.auth.verifyPhoneNumber(number,30000).then(function (smsCodeAndVerificationId) { // break smsCodeAndVerificationId to get sms Code and VerificationId })

after getting smsCode and VerificationId use firebase javascript function

   // get credential using VerificationId and smsCode
   var credential = firebase.auth.PhoneAuthProvider.credential(VerificationId., smsCode);

   // sign in with the gotten credential  and you are done yo !!!!!!
    firebase.auth().signInWithCredential(credential).catch(function(error){
    alert(error);
    self.loginStep('number');
    });
coderpradp commented 4 years ago

@guruaugust Could you be more specific with your solution or share the file? I can't seem to get the response to the client side from your code. After getting sms code how do we send it to javascript? The onCodeSent method doesn't seem to trigger.

sitaram-dev commented 4 years ago

@guruaugust Could you be more specific with your solution or share the file? I can't seem to get the response to the client side from your code. After getting sms code how do we send it to javascript? The onCodeSent method doesn't seem to trigger.

Get verification id from plugin function. Then use your verification id and sms code to form credentials

var credential = firebase.auth.PhoneAuthProvider.credential(VerificationId., smsCode);

And use this JavaScript firebase method to sign in. firebase.auth().signInWithCredential(credential)

coderpradp commented 4 years ago

Get verification id from plugin function. Then use your verification id and sms code to form credentials

var credential = firebase.auth.PhoneAuthProvider.credential(VerificationId., smsCode);

And use this JavaScript firebase method to sign in. firebase.auth().signInWithCredential(credential)

@guruaugust how do you get the verification id and sms code from plugin? As I said, I have called credential.getSmsCode() and updated onCodeSent listener as you said on previous comment, however, client side is not receiving sms code and verification id because onCodeSent is not being triggered.