firebase signInWithEmailAndPassword not working in safari

Viewed 31

When I use the function signInWithEmailAndPassword(email, password) , this works completely fine on Chrome. However, when I go to the same website and use the same credentials on iOS Safari I get this error:

code: "auth/network-request-failed", message: "A network error (such as timeout, interrupted connection or unreachable host) has occurred.

This is the function where the problem is. It is called from the onSubmit attribute of a html form.

  function formSubmit() {
  
    var config = {
      // config here
    };
    var app = firebase.initializeApp(config);

    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        console.log("firebase user is: " + firebase.auth().currentUser.uid); 
        return true; 
      } else {
        console.log("No user")
      }
    });

    var email = document.getElementById("emailInput").value;
    var password = document.getElementById("passwordInput").value;
  
    firebase.auth().signInWithEmailAndPassword(email, password)
    .then(function(userReturn) {
      console.log("userReturn is " + userReturn);
    })
    .catch(function(error) {
      console.log(error);
    })

  }
1 Answers

You are performing a lot of operations within the formSubmit handler. This may be causing the timeout.

It is better practice to initialize the firebase app globally.

In your .js file:


    // init firebase globally

    var config = {
      // config here
    };
    var app = firebase.initializeApp(config);

    // this will trigger as soon as an auth state change operation happens
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        console.log("firebase user is: " + firebase.auth().currentUser.uid); 
        return true; 
      } else {
        console.log("No user")
      }
    });

  // sign users in with the formSubmit handler
  function formSubmit() {

    var email = document.getElementById("emailInput").value;
    var password = document.getElementById("passwordInput").value;
  
    firebase.auth().signInWithEmailAndPassword(email, password)
    .then(function(userReturn) {
      console.log("userReturn is " + userReturn);
    })
    .catch(function(error) {
      console.log(error);
    })

  }
Related