Firebase.auth (). onAuthStateChanged Not Working

I am trying to create a website that uses email authorization for users. However, every time I try to register or log in, the firebase.auth () function. OnAuthStateChanged fires, but does not recognize that the user has registered or registered. This is my current code. I know this works because it will warn me: "No user!" after every login or registration and because I can log in to the Firebase console and see that the user has registered. If anyone knows how to fix this, I would appreciate it!

Thanks!

Code:

function initApp() { firebase.auth().onAuthStateChanged(function(user) { if (user) { alert("Signed in user!") } else { alert("No user!") } }); } window.onload = function() { initApp(); }; 

CODE FOR ENTRANCE AND SIGNAL:

 function toggleSignIn() { if (firebase.auth().currentUser) { alert("Sign out") firebase.auth().signOut(); // [END signout] } else { var email = document.getElementById('email').value; var password = document.getElementById('pass').value; if (email.length < 4) { alert('Please enter an email address.'); return; } if (password.length < 4) { alert('Please enter a password.'); return; } // Sign in with email and pass. // [START authwithemail] firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // [START_EXCLUDE] if (errorCode === 'auth/wrong-password') { alert('Wrong password.'); } else { console.error(error); } // [END_EXCLUDE] }); // [END authwithemail] } } function handleSignUp() { var email = document.getElementById('semail').value; var password = document.getElementById('spass').value; if (password.length < 6) { alert('Password must be 6 characters or more!'); return; } // Sign in with email and pass. // [START createwithemail] firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // [START_EXCLUDE] if (errorCode == 'auth/weak-password') { alert('The password is too weak.'); } else { console.error(error); } // [END_EXCLUDE] }); // [END createwithemail] } 
+5
source share
1 answer

It seems like my problem was that my domain was not allowed for OAuth operations for this Firebase project ... I forgot to add my domain to the authorized list.

To fix this, go to the Firebase project> Authentication> Sign-In Method> Add your domain to Authorized Domains.

Thanks @Ymmanuel for the help.

+1
source

All Articles