Flutterfire: [firebase_auth] Cannot use `FirebaseUser.getIdToken()` when offline

Created on 13 Oct 2019  Â·  7Comments  Â·  Source: FirebaseExtended/flutterfire

Steps to Reproduce

We're having an issue with our app using Firebase as the backend, where our users cannot open the app if they are not connected to the internet. The problem comes from trying to call FirebaseUser#getIdToken(refresh: false) which rejects with an error when it's unable to query the remote server:

2019-06-24 15:57:50.482953+0200 Runner[2833:1012137] TIC TCP Conn Failed [3:0x28388f3c0]: 1:50 Err(50)
2019-06-24 15:57:50.484007+0200 Runner[2833:1012137] Task <C4FE4792-F4B4-44D4-9415-B8D3054BFDB5>.<1> HTTP load failed (error code: -1009 [1:50])
2019-06-24 15:57:50.484134+0200 Runner[2833:1012123] Task <C4FE4792-F4B4-44D4-9415-B8D3054BFDB5>.<1> finished with error - code: -1009
2019-06-24 15:57:50.484334+0200 Runner[2833:1012137] Task <C4FE4792-F4B4-44D4-9415-B8D3054BFDB5>.<1> load failed with error Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x2803f0900 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <C4FE4792-F4B4-44D4-9415-B8D3054BFDB5>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <C4FE4792-F4B4-44D4-9415-B8D3054BFDB5>.<1>"
), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://securetoken.googleapis.com/v1/token?key=REDACTED, NSErrorFailingURLKey=https://securetoken.googleapis.com/v1/token?key=REDACTED, _kCFStreamErrorDomainKey=1} [-1009]
2019-06-24 15:57:50.488412+0200 Runner[2833:1012131] [VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: PlatformException(ERROR_NETWORK_REQUEST_FAILED, Network error (such as timeout, interrupted connection or unreachable host) has occurred., null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
#1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:302:33)
<asynchronous suspension>
#2      FirebaseUser.getIdToken (package:firebase_auth/src/firebase_user.dart:41:10)
<asynchronous suspension>

We are reading the token since we have the Firebase Database id of our data related to the user stored there, using the "custom user claims" feature.

I was under the impression that the Firebase data was to be persisted and work offline as well without any modifications to the code, but maybe this doesn't extend to the user token? It would be very beneficiary for us if it did though, since we otherwise need to have two different cache mechanisms storing related data, which is a recipe for out-of-sync bugs...

  1. Create a login page with Firebase Auth
  2. Login in the app
  3. Turn on flight-mode
  4. Call FirebaseUser#getIdToken(refresh: false)

Logs

2019-06-24 15:57:50.482953+0200 Runner[2833:1012137] TIC TCP Conn Failed [3:0x28388f3c0]: 1:50 Err(50)
2019-06-24 15:57:50.484007+0200 Runner[2833:1012137] Task <C4FE4792-F4B4-44D4-9415-B8D3054BFDB5>.<1> HTTP load failed (error code: -1009 [1:50])
2019-06-24 15:57:50.484134+0200 Runner[2833:1012123] Task <C4FE4792-F4B4-44D4-9415-B8D3054BFDB5>.<1> finished with error - code: -1009
2019-06-24 15:57:50.484334+0200 Runner[2833:1012137] Task <C4FE4792-F4B4-44D4-9415-B8D3054BFDB5>.<1> load failed with error Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x2803f0900 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <C4FE4792-F4B4-44D4-9415-B8D3054BFDB5>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <C4FE4792-F4B4-44D4-9415-B8D3054BFDB5>.<1>"
), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://securetoken.googleapis.com/v1/token?key=REDACTED, NSErrorFailingURLKey=https://securetoken.googleapis.com/v1/token?key=REDACTED, _kCFStreamErrorDomainKey=1} [-1009]
2019-06-24 15:57:50.488412+0200 Runner[2833:1012131] [VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: PlatformException(ERROR_NETWORK_REQUEST_FAILED, Network error (such as timeout, interrupted connection or unreachable host) has occurred., null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
#1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:302:33)
<asynchronous suspension>
#2      FirebaseUser.getIdToken (package:firebase_auth/src/firebase_user.dart:41:10)
<asynchronous suspension>
$ flutter analyze
Analyzing done-flutter...                                               
No issues found! (ran in 2.2s)

(I'm only running on iOS at the moment)

$ flutter doctor -v
[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.5 18F203, locale en-SE)
    • Flutter version 1.5.4-hotfix.2 at /Users/linus/coding/flutter
    • Framework revision 7a4c33425d (8 weeks ago), 2019-04-29 11:05:24 -0700
    • Engine revision 52c7a1e849
    • Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/setup/#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, set ANDROID_HOME to that location.
      You may also want to add it to your PATH environment variable.


[✓] iOS toolchain - develop for iOS devices (Xcode 10.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.2.1, Build version 10E1001
    • ios-deploy 1.9.4
    • CocoaPods version 1.7.2

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/setup/#android-setup for detailed instructions).

[✓] VS Code (version 1.35.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.1.0

[✓] Connected device (1 available)
    • Linus Unnebäck’s iPhone • f94aa6361e142ab9641a700b2eb487e8611738f4 • ios • iOS 12.3.1

! Doctor found issues in 2 categories.
customer-response customer auth needs-repro bug

All 7 comments

@LinusU

The issue at https://github.com/flutter/flutter/issues/34998 has been closed and moved here. Future collaboration on this issue will be done here.

@LinusU @fatalcaron did you find a solution?

@BondarenkoStas I only need the custom claims so I store the custom claims on Shared Preference when I get the token ID while the user is online. I create a function that get the into this shared preference if the custom claims is not available in offline mode.

Same problem. If I call getIdToken I get exceptions and my app doesn't launch. Do I need to check if I'm offline before calling this? How do I know if I can safely call this? If I pass refresh: true, it always fails, if I don't pass refresh, it will work until it needs to refresh, then crashes.

its working correctly for me i'm using firebase_auth: 0.14.0+5
and i can use getIdToken() offline

I'm getting a similar problem.
After a user successfully logins in, and I attempt to verify the users token, the call getIdToken on a firebase user just does not return any concrete thing. I even don't observe any issues or errors.

This happens only on Android, iOS works correctly.
firebase_auth: ^0.15.5+2

Hey 👋

Our rework of the firebase_auth plugin as part of the FlutterFire roadmap was published over a week ago with a ton of fixes and new features. Please could you try the new version and see if this is still an issue for you? If it is then please submit a new up to date GitHub issue.

For help migrating to the new plugins please see the new migration guide: https://firebase.flutter.dev/docs/migration

Was this page helpful?
0 / 5 - 0 ratings