0
votes

i had tried Firebase 3.5 and 3.6 but have same error. I had read some toipc and i had removed type="submit" in form and add domain in project but it always reports . I had tested it with Firefox 50 and Chrome 54 - I create it in web. My flow is: i call ajax and receive data - If data is true, it will return json include token and sucess = 1. After Ajax call, if have data and data.sucess = 1, i will auth with data.token

auth/network-request-failed

Thrown if a network error (such as timeout, interrupted connection or unreachable host) has occurred.

<form id="form-login" action="" enctype="application/x-www-form-urlencoded">
<!-- Username -->
    <input id="username" name="username" type="text" size="20"  />
<!-- Password -->
    <input id="password" name="password" autocomplete="off" type="password" size="20"  />
<input id="btn-login" class="h_submit" value="Login" />
$('#btn-login').click(function(event) {
        event.preventDefault();
        
        var username = $.trim(jInputUsername.val());
        var password = $.trim(jInputPassword.val());
        if (!username || !password) {
            alert("Error");
        } else {
            
            $.ajax({
                url: '', 
                type: "post",
                data: {'username': username, 'password': password}, 
                success: function(data, textStatus, jqXHR) {
                    //        
                    if ($.isPlainObject(data)) {
                        //    On error
                        if (data && data.succeed) {
                            firebase.auth().signInWithCustomToken(data.token).catch(function(error) {
                              var errorCode = error.code;
                              var errorMessage = error.message;
                              if (errorCode === 'auth/invalid-custom-token') {
                                alert('Token problem.');
                              }
                              alert(errorMessage);
                            });
                            var fullHref = decodeURI(window.location.href);
                            setTimeout(window.location.href = fullHref.replace(hash, ''), 1000);
                        }
                    }
                },
                error: function( jqXHR, textStatus) {
                    console.log('Username or passord wrong");
                }
                , dataType:'json'
            });
        }
    });
1
You could also use the ".then" promise like in following: firebase.auth().signInWithEmailAndPassword(email, password) .then(user => console.log(user)) .catch((error) => console.log(error));illuminate

1 Answers

2
votes

This error arises because the Auth object takes time to initialise. Though you are using a timeout but that does not guarantee the successful initialisation.

According to Firebase docs here the recommeded way to check whether sign in is successful and get the current user is to set an observer on the Auth object. So to resolve this error do the redirection window.location.href inside the onAuthStateChanged observer.

Hope this helps!!