chemerisuk / cordova-plugin-firebase-authentication

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

signInWithVerificationId does not work properly #20

Open osenel opened 6 years ago

osenel commented 6 years ago

Hello,

I am trying to sign in with SMS on my project. My code with comments is following.

`function SendSMS(){ var phoneNumber=String(document.getElementById("inputPhoneNumber").value);

cordova.plugins.firebase.auth.verifyPhoneNumber(phoneNumber).then(function(verificationId) { SMS_code = prompt("Please enter the code: ", ""); //this async function waits until user gives an input

alert("SMS_code: "+SMS_code+"\n verificationId: "+verificationId);       // I can see both code and verification ID 
cordova.plugins.firebase.auth.signInWithVerificationId(verificationId, SMS_code).then(
         user => {
            alert("user: "+user+"\n user.uid: "+user.uid);    // user: [object Object] and user.uid: xyz... this line also works fine!                      
                                NameListRef="CompleteNameList";
                                firebase.database().ref(NameListRef).once('value').then( function(snapshot){
                                NameList=snapshot.val(); 
                                alert("NameList: " +NameList);
                                },
                                function(error){
                                alert("error: " +error); //Error: permission_denied. client does not have permission to access the desired data. 
                                });                 
                }, error=>{
                    alert("error: "+error);
                }); 

}); }`

The only rule for my database is authentication. After this point, I have checked whether I am signed in. I have added a listener and a function (fired by a button).

`cordova.plugins.firebase.auth.onAuthStateChanged(function(userInfo) {
if (userInfo && userInfo.uid) { alert("user signed in"); // THIS ALERT NEVER FIRES } else { alert("user signed out"); } });

function checkUser(){ // called by button currentUserUID= firebase.auth().user; alert("currentUserUID: " +currentUserUID); //UNDEFINED }`

PS: I have added the correct package name and SHA-1 in the Firebase Console PS2: I can see that new user is created at Firebase console and "last sign in time" is updated.

Thanks...

donvie commented 6 years ago

We have same problem, Have you fixed it, Share your solutions to me Thansk :-)

osenel commented 6 years ago

Hello, I have solved the issue by using another method which is described in [https://github.com/arnesson/cordova-plugin-firebase] (I have NOT install arnesson's plugin) Here is my code:

`var VerifId; cordova.plugins.firebase.auth.verifyPhoneNumber(EnteredPhoneNum).then(function(verificationId) { VerifId=verificationId; EnterCode();
}); function EnterCode(){

SMS_code = prompt("Please enter the code in SMS ", "");
try { var credential = firebase.auth.PhoneAuthProvider.credential(VerifId, SMS_code); } catch(err) {alert("PhoneAuthProvider.credential sonrası catch error: "+err.message);}

firebase.auth().signInWithCredential(credential).then(function(error){ alert("signInWithCredential error: "+error.message); }); } `

donvie commented 6 years ago

@osenel Thanks got it <3

msuhaibk commented 5 years ago

i have the same issue.

cordova.plugins.firebase.auth.onAuthStateChanged(function(userInfo) {
if (userInfo && userInfo.uid) {
alert("user signed in"); // THIS ALERT FIRES IN MY CASE
} else {
alert("user signed out");
}
});

function checkUser(){ // called by button
currentUserUID= firebase.auth().user;
alert("currentUserUID: " +currentUserUID); //THIS DOES NOT 
}

there is an incompatibility for this plugin with the official firebase plugin.

innovativethinkingteam commented 4 years ago

Same issue as above- from onAuthStateChanged I get userInfo & userInfo.uid but as soon as I attempt a write I get permission denied errors (As my rules are set to write if request.auth).

Is there any work around or patch that can be implemented until this is resolved?