4

In my Flutter app I'm using the Firebase Auth package for user authentication. When I sign in everything seems to work for a while. But after a while (not sure if it's exactly 1 hour) I get the following messages in my debug console:

W/Firestore( 4587): (24.0.0) [FirestoreCallCredentials]: Failed to get auth token: com.google.firebase.FirebaseException: An internal error has occurred. [ INVALID_REFRESH_TOKEN ]. W/Firestore( 4587): (24.0.0) [WatchStream]: (3351d52) Stream closed with status: Status{code=UNAUTHENTICATED, description=null, cause=com.google.firebase.FirebaseException: An internal error has occurred. [ INVALID_REFRESH_TOKEN ]

From that moment on I can't access any Firebase Services. The way to fix this is to sign out and sign back in.

Not sure if this behaviour would occur in production as well as I'm using Firebase Emulators for Auth and Firestore in development.

Lara
  • 84
  • 1
  • 7
  • 30

2 Answers2

0

There can be multiple reasons for this error. I am just listing some pointers for you to check :

Enable :

  1. This can be caused by a disabled Email/Password sign in, which is disabled by default. Enable the Authentication section in the Firebase console as shown below or you can temporarily try the "anonymously" / guest sign in in Firebase Console.

  2. Enable the Token Services API in Google Cloud Console

  3. Enable Cloud Firestore API

enter image description here

If you are using the local Authentication emulator, then it is possible to let your app talk to it by connecting to this using the useAuthEmulator method. In Flutter libraries, this has landed in version 0.20 and later of the firebase_auth package.

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();

// Ideal time to initialize
await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);
//…
}

If you are using the local Firestore emulators, then it is possible to let your app talk to it by connecting to this using the useFirestoreEmulator method.

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  // Ideal time to initialize
  FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
  //...
}
  • Try upgrading to the latest version of firebase.
  • Ensure you pass the correct port on which the emulator is running on.
  • Ensure you have enabled network connections to the emulators in your apps following the emulator usage instructions in the general FlutterFire installation notes for each operating system.

Update :

For anyone visiting this question, these pointers above did not help our OP for her case, what helped her was, compare current google-services.json from the project with the one that the Firebase console generates, after you generate the SHA-1 Key (to create an OAuth2 client and API key for your app. Check this stackoverflow thread it explains how you can add SHA-1 Key. If you want to do this using terminal, have a look at this article) And it turned out they were not quite the same. So adding the new google-service.json to project under android/app and executing flutter clean was the trick.

Priyashree Bhadra
  • 3,182
  • 6
  • 23
  • I'm only using Google sign in which is enabled in the Firebase console. The APIs you mentioned are enabled as well. So I can login (auth) and manipulate Firestore data. It's just after a while of idling in the app that this error appears. I am using local emulators and they are connected properly (I can tell because everything works fine for the first hour ish after a sign in). Using the latest version of firebase as well – Lara Jan 09 '22 at 09:15
  • Am I supposed to manually refresh the session or something? I thought this package would manage it all for me. – Lara Jan 09 '22 at 09:35
  • Firebase Authentication sessions are long lived. Every time a user signs in, the user credentials are sent to the Firebase Authentication backend and exchanged for a Firebase ID token (a JWT) and refresh token. Firebase ID tokens are short lived and last for an hour; the refresh token can be used to retrieve new ID tokens. Refresh tokens expire only when one of the following occurs: The user is deleted The user is disabled A major account change is detected for the user. This includes events like password or email address updates. – Priyashree Bhadra Jan 09 '22 at 10:26
  • I understood what the issue is. Firebase auth module has auto refresh mechanism internally. So you don’t need to care about fetching token.When you log in to firebase auth, firebase generate token and use it to perform operation on Firebase Database. After 1 hour, token is expired and Firebase Auth SDK understand that it needs to refresh token. I think the issue is about SHA-1 Key.You have to add SHA-1 key to Firebase Console. Otherwise your token is expired after 1 hour and not going to refresh automatically. – Priyashree Bhadra Jan 09 '22 at 10:37
  • This is an important step. To be able to use Google sign-in, then you need to generate the SHA-1 key. This key will be used by Firebase to create an OAuth2 client and API key for your app. Check this [stackoverflow thread](https://stackoverflow.com/a/56091158/15803365) it explains how you can add SHA-1 Key. If you want to do this using terminal, have a look at this [article](https://medium.com/firebase-tips-tricks/how-to-use-google-sign-in-with-firebase-in-flutter-bfc4d0e463e7), jump to Generate SHA-1 Fingerprint section, you will see the detailed steps. – Priyashree Bhadra Jan 09 '22 at 10:44
  • I've already had both SHA-1 and SHA-256 fingerprints of my `:app:signingReport` (variant `debug` config `debug` ) added to my app in the Firebase console. – Lara Jan 09 '22 at 10:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/240884/discussion-between-priyashree-bhadra-and-lara). – Priyashree Bhadra Jan 09 '22 at 11:47
  • I have the exact same issue. I have compared `google-services.json` I've downloaded from remote Firebase and local `android/app/google-services.json` but there's no difference. I've changed and recompiled again to try it out and does not work. Will do `flutter clean` to see the results. – Eray Erdin Apr 11 '22 at 12:12
  • `flutter clean` also did not do the trick. :/ – Eray Erdin Apr 12 '22 at 10:55
  • got the same Error but on the web when running Cypress automated tests. it wasn't happening before. – Mostafa Hesham Jun 08 '22 at 00:38
0

Another reason this might happen is if you close the emulator process on your computer.

Firebase emulator clears data when firebase emulators:start is finished.

So the OP might have restarted their PC/macbook during the work, which would explain it.

I decided to post it here because somebody might stumble upon this question and not think about it as much as I did.

Konstantin Kozirev
  • 944
  • 1
  • 10
  • 23