firebase - onAuthStateChanged is invoked twice while the function in which it resides is called only once -


function signinwithgoogle(){ //console.log("clicked signinwithgoogle"); firebase.auth().onauthstatechanged(function(user){     if(user){         alert("already logged in");     }     else{         // var info=document.getelementbyid("info");         var provider=new firebase.auth.googleauthprovider();         firebase.auth().signinwithpopup(provider).then(function(result){             if(result.credential){                 var token=result.credential.accesstoken;                  console.log("token="+token);             }             var user=result.user;             console.log("user_provider="+user.displayname+" user_email="+user.email+" user_dp="+user.photourl+" user_verification="+user.emailverified+" uid="+user.uid);         }).catch(function(error){             var errorcode=error.code;             var errormessage=error.message;             var email=error.email;             var credential=error.credential;             alert(errormessage);         });     } }); } 

when user signs in first time, sign-in process goes after alert message("already logged in") shown, indicates onauthstatechanged called once again.how make not call again?

i sure that signinwithgoogle() invoked once.

p.s. have seen many questions related don't have concrete solution.

onauthstatechanged called first time when user not signed in, signinwithpopup. causes user sign in, triggering same observer again , hence alert message. agree @camden_kid's assessment. if insist on adding observer in sign in function, can unsubscribe when first called:

var unsubscribe = firebase.auth().onauthstatechanged(function(user){   unsubscribe();   ... }); 

this guarantee called once each time call function.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -