简体   繁体   中英

Flutter Firebase Auth - INVALID_REFRESH_TOKEN (Firebase Emulator)

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.

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

在此处输入图像描述

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.

For more detailed guidance, have a look at this documentation on how to configure and integrate Emulators and try the solutions posted by me in this stackoverflow thread if this answer does not help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM