Skip to content

How to handle firebase auth errors? #20223

Closed
flutter/plugins
#775
@riccardoratta

Description

@riccardoratta

I'm trying to build a login page with firebase auth plugin. But I don't know how to handle auth errors in a cross-platform way. For example, this are some PlatformException that I get when the user type the wrong password/email or there are a network issue.

On iOS:

PlatformException(Error 17011, FIRAuthErrorDomain, There is no user record corresponding to this identifier. The user may have been deleted.)

PlatformException(Error 17009, FIRAuthErrorDomain, The password is invalid or the user does not have a password.)

PlatformException(Error 17020, FIRAuthErrorDomain, Network error (such as timeout, interrupted connection or unreachable host) has occurred.)

On Android:

PlatformException(exception, There is no user record corresponding to this identifier. The user may have been deleted., null)

PlatformException(exception, The password is invalid or the user does not have a password., null)

PlatformException(exception, A network error (such as timeout, interrupted connection or unreachable host) has occurred., null)

The format is: code, message, details. In Android I get a code: exception, what does it mean? And also in iOS I always get a message: FIRAuthErrorDomain and not any standard code listed here.

The code:

try {
    _user = await _auth.signInWithEmailAndPassword(
        email: _email,
        password: _password);
} on PlatformException catch (e) {
    print(e);
}

Temporary fix (don't rely on this solution)

In the meantime the problem can be fixed (see bottom for warning on this solution) with something like that:

enum authProblems { UserNotFound, PasswordNotValid, NetworkError }
authProblems errorType;
if (Platform.isAndroid) {
  switch (e.message) {
    case 'There is no user record corresponding to this identifier. The user may have been deleted.':
      errorType = authProblems.UserNotFound;
      break;
    case 'The password is invalid or the user does not have a password.':
      errorType = authProblems.PasswordNotValid;
      break;
    case 'A network error (such as timeout, interrupted connection or unreachable host) has occurred.':
      errorType = authProblems.NetworkError;
      break;
    // ...
    default:
      print('Case ${e.message} is not jet implemented');
  }
} else if (Platform.isIOS) {
  switch (e.code) {
    case 'Error 17011':
      errorType = authProblems.UserNotFound;
      break;
    case 'Error 17009':
      errorType = authProblems.PasswordNotValid;
      break;
    case 'Error 17020':
      errorType = authProblems.NetworkError;
      break;
    // ...
    default:
      print('Case ${e.message} is not jet implemented');
  }
}
print('The error is $errorType');

Warning: The problem, that I would like to emphasize with this issue, it's that you can't rely too much in this solution because a simple FirebaseAuth update can screw it up. Also if you choose this way be sure to lock the firebase_auth: ?.?.? to a specific version in the pubspec.yaml file.


Flutter doctor

[✓] Flutter (Channel beta, v0.5.1, on Mac OS X 10.13.6 17G65, locale en-IT)
    • Flutter version 0.5.1 at /Users/.../Library/flutter
    • Framework revision c7ea3ca377 (10 weeks ago), 2018-05-29 21:07:33 +0200
    • Engine revision 1ed25ca7b7
    • Dart version 2.0.0-dev.58.0.flutter-f981f09760

[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
    • Android SDK at /Users/.../Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-27, build-tools 27.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
    • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 9.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 9.4.1, Build version 9F2000
    • ios-deploy 1.9.2
    • CocoaPods version 1.5.3

[✓] Android Studio (version 3.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[✓] IntelliJ IDEA Community Edition (version 2017.3.5)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 25.0.1
    • Dart plugin version 173.4700

[!] VS Code (version 1.25.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected devices (1 available)
    • Nexus 5X • 01ce0d68fc7ca517 • android-arm64 • Android 8.1.0 (API 27)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions