firebase.auth().onAuthStateChanged Not Working

25,513

Solution 1

It appears that my problem was my domain was not authorized for OAuth operations for that Firebase project... I had forgotten to add my domain to the list of authorized ones.

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

Thanks to @Ymmanuel for the help.

Solution 2

For those poor souls working with react-native-firebase on iOS, there is no need to struggle any more.

  1. Make sure you have added the generated Google plist to your project. 2.Make sure you have generated and added your APN file to your firebase account.
  2. Make sure the app bundle id you have set in Firebase consile matches with that in xcode.
Share:
25,513

Related videos on Youtube

Collin
Author by

Collin

// coder

Updated on February 02, 2022

Comments

  • Collin
    Collin about 2 years

    I'm trying to build a website that uses email auth for users. However, everytime I try to sign up or log in a user, the firebase.auth().onAuthStateChanged function fires, but doesn't recognize that a user has logged in or signed up. This is my current code. I know it works because it will alert me, "No user!" after every log in or sign up and because I can go into my Firebase console and see that the user has signed up. 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 LOGIN & SIGNUP:

    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]
    }
    
    • Ymmanuel
      Ymmanuel almost 8 years
      can you put more of your code... ??? to understand how and when you do the login and sign in
    • Collin
      Collin almost 8 years
      I've added it to the original question. Both functions are fired through button clicks.
    • Collin
      Collin almost 8 years
      It appears this is a common problem with no answer as of now... :/ stackoverflow.com/questions/37504466/…
    • Ymmanuel
      Ymmanuel almost 8 years
      i've used both methods correctly but with react and angular... i'll check how they behave with pure javascript
    • Collin
      Collin almost 8 years
      Hmmmm, looking in the console, I now see this error: Error: This domain is not authorized for OAuth operations for your Firebase project. Edit the list of authorized domains from the Firebase console. Is there a way to fix?
    • Ymmanuel
      Ymmanuel almost 8 years
      Yep you need to go to your project in your firebase console and add the domain to the authorised domains in authentication menu->sign-in-method , by default localhost is authorised, or you can add your local IP if you are testing locally
    • Collin
      Collin almost 8 years
      Awesome, thanks. It works now! :)
    • Ymmanuel
      Ymmanuel almost 8 years
      if you have something like "admin.mydomain.com" you just need to add "my domain.com"
    • FinestStudios
      FinestStudios about 2 years
      Does this answer your question? Firebase auth gets stuck on iOS login without error
  • edgsv
    edgsv about 6 years
    This was exactly my problem and also my solution thanks!