Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Authentication FirebaseNetworkException: A network error (such as timeout, interrupted connection or unreachable host) has occurred

I'm creating an authentication workflow for my android app. I'm allowing users to sign in with username/password and various OAuth providers. I'm validating emails and password so that, I know the information I'm passing to Firebase is valid. I'm using com.google.firebase:firebase-auth:9.6.1

When I execute the following code, I get a callback that says the operation not successful with an error.

mFirebaseAuth.signInWithEmailAndPassword(username,password).addOnCompleteListener(this);

Callback function or completion listener tells me

com.google.firebase.FirebaseNetworkException: A network error (such as timeout, interrupted connection or unreachable host) has occurred.

The username I'm passing doesn't exist yet. So, I would assume to see some kind of error stating the user doesn't exist. Am I passing something incorrectly or am I assuming incorrectly? I can also see that in the Firebase documentation, the iOS library has a various error codes common to all API methods section where as the Android section doesn't show this. One of these exceptions is FIRAuthErrorCodeUserNotFound. So, does that functionality even exists in the Android library?

like image 909
Landen Avatar asked Oct 14 '16 21:10

Landen


5 Answers

It can also happen when google play services are not running. Try to launch play store and check if it is working. If not reboot of device issue.And also compare the google play services using in the project and google play services in the device are same if not update google play services.

This is just a minor but possible case where it gives the exception.

like image 61
Tej Avatar answered Nov 18 '22 05:11

Tej


changing from <form></form> to <div></div> solved this problem:

"A network error (such as timeout, interrupted connection or unreachable host) has occurred in a form element in the HTML. Small bug."

like image 39
Dan Alboteanu Avatar answered Nov 18 '22 05:11

Dan Alboteanu


In your AndroidManifest.xml add, it works for me

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
like image 4
Kangudie Muanza - Killmonger Avatar answered Nov 18 '22 06:11

Kangudie Muanza - Killmonger


<a (click)="login()" class="nav-link">Login</a>

Do not put href attribute into tag a. It is help to solved my case

like image 3
Huy - Logarit Avatar answered Nov 18 '22 06:11

Huy - Logarit


If you do this check in form onSumbit handler, you need to preventDefault before sending a request.

This is a snippet (React) that works:

class LoginComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            email: '',
            password: '',
        };
        this.login = this.login.bind(this);
        this.handleLoginChange = this.handleLoginChange.bind(this);
        this.handlePasswordChange = this.handlePasswordChange.bind(this);
    }

    handleLoginChange(event) {
        this.setState({
            email: event.target.value,
            password: this.state.password,
        });
    }

    handlePasswordChange(event) {
        this.setState({
            email: this.state.email,
            password: event.target.value,
        });
    }

    login(event) {
        event.preventDefault();
        firebase.auth()
            .signInWithEmailAndPassword(this.state.email, this.state.password)
            .then(function(user) {
                      window.alert('OK' + user);
                  },
                  function(error) {
                      window.alert('ERR' + error);
                  });
    }

    render() {
        return (
            <form onSubmit={this.login}>
                <TextField hintText="login" value={this.state.email} onChange={this.handleLoginChange} /><br/>
                <TextField hintText="password" type="password" value={this.state.password} onChange={this.handlePasswordChange} /><br/>
                <RaisedButton type="submit" label="Login" primary={true} />
            </form>
        )
    }
}
like image 3
Konstantin Nikitin Avatar answered Nov 18 '22 07:11

Konstantin Nikitin