Landen
Landen

Reputation: 529

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?

Upvotes: 34

Views: 60784

Answers (17)

benjiman
benjiman

Reputation: 4048

Another possible reason might be, that the clients that are getting this error are sending request from China, because all URLs such as *.google.com and *.googleapis.com are blocked in China.

Apparently Firebase has a China service at https://firebase.google.cn, but I didn't have chance to check it out yet. You can also read more on this in this Stackoverflow post: https://stackoverflow.com/a/42620514/1372783

Upvotes: 0

V.K.Agarwal
V.K.Agarwal

Reputation: 19

I have faced this problem after searching a lot i got the solution, In my case it was happening due to the restriction of background data.


If you turn off background data of Google Service Framework. Check & turn on it. It worked for me.

Upvotes: 0

Michael Nelles
Michael Nelles

Reputation: 5992

It could be a million things my advice based on my experience is check the firebase logs and work the problem from there.

In my case I was hitting the endpoint successfully but it was generating a catch error because of an invalid variable rowsCompany on the res.json({...response...} )

enter image description here

Upvotes: 0

RvSingh3213
RvSingh3213

Reputation: 189

I have also got the same error and there are sometimes very small things which we forget Like -> Turn On Internet as

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

This is FirebaseNetwordException there might be case when Host is not Reachabe like mentioned above so check you connectivity

aslo check weather you have provided Internet Permissions in Manifest or not.

hope it will solve your problem

Upvotes: 0

Emre Kilinc Arslan
Emre Kilinc Arslan

Reputation: 2189

  1. Reason is Simple ! Check if Device has internet connection.
  2. if you have google services enabled
  3. phone time settings
  4. firebase json file is wrong .cant take client id or google app id or api key

Upvotes: 6

Simon
Simon

Reputation: 6462

If you are using Genymotion, it could be that you did not activate Open Gapps (in the top right corner)

Upvotes: 0

atschpe
atschpe

Reputation: 121

I had a similar problem accessing firebase. In my case, the problem was that I was doing this on a real device via the debugger. Once I ran the code "normally" the error vanished. So, it might be helpful to check whether the debugger is getting in the way.

Upvotes: 0

inorganik
inorganik

Reputation: 25525

Can also happen if your authDomain is set improperly in your firebase keys for web projects.

Upvotes: 0

Dan Alboteanu
Dan Alboteanu

Reputation: 10232

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."

Upvotes: 25

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"/>

Upvotes: 3

Sathish
Sathish

Reputation: 1521

In my case the issue is due to mismatch versions of dependencies, then I have changed dependencies like below

implementation 'com.google.firebase:firebase-auth:+'
implementation 'com.google.firebase:firebase-core:+'
implementation 'com.google.android.gms:play-services-auth:+'
implementation 'com.firebaseui:firebase-ui-auth:+'

then its started working perfectly.

Upvotes: 0

luishsivla
luishsivla

Reputation: 11

I resolve the problem fixing the date of my cell phone were one year late, when I fixed the date, everything worked well again and the error disappear, I hopping this help you

Upvotes: 1

Konstantin Nikitin
Konstantin Nikitin

Reputation: 2606

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>
        )
    }
}

Upvotes: 3

ZuzEL
ZuzEL

Reputation: 13645

Check your rewrites in firebase.json, make sure you don't rewrite auth provider url /__/

{
  "database": {
    "rules": "database.rules.json"
  },
  "storage": {
    "rules": "storage.rules"
  },
  "hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "!/__/**",
        "destination": "/index.html"
      },
      {
        "source": "**/!(*.js|*.html|*.css|*.json|*.svg|*.png|*.jpg|*.jpeg)",
        "destination": "/index.html"
      }
    ]
  }
}

Also it could be service worker problem. See https://github.com/Polymer/polymer-cli/issues/290

Upvotes: 0

Huy - Logarit
Huy - Logarit

Reputation: 686

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

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

Upvotes: 3

Tej
Tej

Reputation: 604

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.

Upvotes: 23

Chetan9007
Chetan9007

Reputation: 908

I was facing the same issue. what solved my problem by clear extra space in API_KEY, so my suggestion is to check your GoogleService-Info.plist for

  1. API_KEY is proper (without extra spaces)
  2. GOOGLE_APP_ID
  3. CLIENT_ID

I think this might help you

Upvotes: 2

Related Questions