Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle firebase auth errors? #20223

Closed
riccardoratta opened this issue Aug 4, 2018 · 36 comments · Fixed by flutter/plugins#775
Closed

How to handle firebase auth errors? #20223

riccardoratta opened this issue Aug 4, 2018 · 36 comments · Fixed by flutter/plugins#775
Labels
p: firebase Firebase plugins package flutter/packages repository. See also p: labels. waiting for PR to land (fixed) A fix is in flight

Comments

@riccardoratta
Copy link
Contributor

riccardoratta commented Aug 4, 2018

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)
@zoechi
Copy link
Contributor

zoechi commented Aug 19, 2018

Similar to #18312, #13027

@zoechi zoechi added plugin p: firebase Firebase plugins labels Aug 19, 2018
@joshkendrick
Copy link

i think this issue is the appropriate spot...
i'm seeing

PlatformException(exception, The sms verification code used to create the phone auth credential is invalid. Please resend the verification code sms and be sure use the verification code provided by the user., null)

if a user enters the wrong SMS code when doing phone number auth -- it looks like this this may be getting addressed in flutter/plugins#678 if it's similar to #18312 ?

@slightfoot
Copy link
Member

@joshkendrick My upcoming patch will address this. and #18312

@joshkendrick
Copy link

sounds great!

@eturk1
Copy link

eturk1 commented Sep 29, 2018

@joshkendrick My upcoming patch will address this. and #18312

@joshkendrick , will your update allow us to catch all Firebase exceptions that currently can't be handled?
(I see many several issues posted about exceptions not handle but only you seem to be addressing it)

@zoechi zoechi added the waiting for PR to land (fixed) A fix is in flight label Sep 29, 2018
BraeWebb referenced this issue in BraeWebb/mediroo Oct 10, 2018
@HerrNiklasRaab
Copy link

Is there any hack, to identify the errors correctly in android? Because now the error code is not provided. :(

@ghost
Copy link

ghost commented Oct 15, 2018

As @HerrNiklasRaab said now it returns this PlatformException(sign_in_failed, FIRAuthErrorDomain, The email address is badly formatted.) how to handle properly?
It used to return specific code but now every errors return sign_in_failed as long as it is about sign in.

@knitero
Copy link

knitero commented Oct 15, 2018

Stuck with the same issue. Any work around?

@anderscheow
Copy link

Any update on this? I need error code to display custom error message

@suparnavg
Copy link

+1.. this is a core part of the firebase auth experience. Really difficult to release an app into the wild where all sorts of auth errors might crop up. The PlatformException thrown freezes up the whole app and makes it unusable until a recompile..

@riccardoratta
Copy link
Contributor Author

@suparnavg Sorry why you are saying that this auth errors freezes up the app and it's not unusable until a recompile? In the code that I've included it doesn't happen. You are maybe forgetting the try {} catch (e) {} statement? If not can you give a more detailed example?

@riccardoratta
Copy link
Contributor Author

riccardoratta commented Nov 9, 2018

@knitero @anderscheow @HerrNiklasRaab See the updated question for a temporary solution.

@HerrNiklasRaab
Copy link

@riccardoratta Thanks, this is bad, but the best possible solution :)

@anderscheow
Copy link

@riccardoratta The solution you proposed is what I wanted to prevent :(

@riccardoratta
Copy link
Contributor Author

riccardoratta commented Nov 9, 2018

@anderscheow Yes, it's the point of the issue. We need a cross platform way (for example by an enum) to know exactly what kind of error firebase auth is throwing. The temporary fix I've included is if you have an application that need to be publicated as soon as possible and you don't know how to proceed.

@slightfoot
Copy link
Member

My fix will be merged soon and this can be resolved.

@anderscheow
Copy link

Is the fix release?

@suparnavg
Copy link

Thanks @riccardoratta will give it a shot. My comment was meant for the original question, not your solution :)

@JonasJW
Copy link

JonasJW commented Jan 13, 2019

Is there meanwhile a solution to this problem?

I need to something like this:

    try {
      await _auth.createUserWithEmailAndPassword(email: email, password: password);
      return 0;
    } catch (error) {
      return error;
    }

So I can distinguish the errors but I could find any solution to this.

@markflarup
Copy link

My fix will be merged soon and this can be resolved.

Any updates on this?

@zoechi
Copy link
Contributor

zoechi commented Jan 29, 2019

@markflarup

This issue is closed.

@slightfoot slightfoot closed this in flutter/plugins#775 on 22 Nov 2018

@ra7bi
Copy link

ra7bi commented Jul 13, 2019

You can do

    try {
      //
    }on PlatformException catch (error)  { 
       List<String> errors = error.toString().split(',');
      print("Error: " + errors[1]); 
    }

You will get the error only like

The email address is badly formatted

@mikecbone
Copy link

On Android, you can grab the code from the exception. I can't test on iOS atm but is there a reason not to use this?

catch (e) {
  print(e.code);
  switch (e.code) {
    case 'ERROR_INVALID_EMAIL':
      authError = 'Invalid Email';
      break;
    case 'ERROR_USER_NOT_FOUND':
      authError = 'User Not Found';
      break;
    case 'ERROR_WRONG_PASSWORD':
      authError = 'Wrong Password';
      break;
    default:
      authError = 'Error';
      break;
  }
}

@slightfoot
Copy link
Member

I implemented the error handling. You do not want to use error.toString() rather as @mikecbone said. Check the code if you want some specific behaviour when an error occurs then you can show a widget or take the user to a specific screen. If you want to display the friendly message. Then you can use error.message, or as @mikecbone has done so you can app specific messages perhaps, or custom localised versions.

https://api.flutter.dev/flutter/services/PlatformException-class.html

@marcelino-borges
Copy link

On Android, you can grab the code from the exception. I can't test on iOS atm but is there a reason not to use this?

catch (e) {
  print(e.code);
  switch (e.code) {
    case 'ERROR_INVALID_EMAIL':
      authError = 'Invalid Email';
      break;
    case 'ERROR_USER_NOT_FOUND':
      authError = 'User Not Found';
      break;
    case 'ERROR_WRONG_PASSWORD':
      authError = 'Wrong Password';
      break;
    default:
      authError = 'Error';
      break;
  }
}

@mikecbone, where can we find all these existing error messages? Firebase official docs don't have it... And i can't find anywhere...

@mikecbone
Copy link

@mikecbone, where can we find all these existing error messages? Firebase official docs don't have it... And i can't find anywhere...

You can find a list of errors here. You just need to replace FIRAuthErrorCode with ERROR and then stick an underline between each word.

So FIRAuthErrorCodeUserNotFound becomes ERROR_USER_NOT_FOUND

Not the best solution I know, but it does seem to work.

@marcelino-borges
Copy link

Oh boy, so that's the secret... Thank you!

@slightfoot
Copy link
Member

slightfoot commented Jul 29, 2019

I documented the error codes in the source on each one of the FirebaseAuth functions.

For example: https://github.com/flutter/plugins/blob/master/packages/firebase_auth/lib/src/firebase_auth.dart#L204

So just check the function comments you are calling.

@marcelino-borges
Copy link

@slightfoot

I'll check it out! Thank you!

@TxusBlack
Copy link

I documented the error codes in the source on each one of the FirebaseAuth functions.

For example: https://github.com/flutter/plugins/blob/master/packages/firebase_auth/lib/src/firebase_auth.dart#L204

So just check the function comments you are calling.

Error 404

@marcelino-borges
Copy link

I documented the error codes in the source on each one of the FirebaseAuth functions.
For example: https://github.com/flutter/plugins/blob/master/packages/firebase_auth/lib/src/firebase_auth.dart#L204
So just check the function comments you are calling.

Error 404

Indeed

@gcamp806
Copy link

Found it - https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_auth/lib/src/firebase_auth.dart#L204

@TxusBlack
Copy link

Found it - https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_auth/lib/src/firebase_auth.dart#L204

Thanks! 😄

@kroikie
Copy link

kroikie commented Oct 13, 2019

@riccardoratta

This issue has been moved to firebase/flutterfire#725. Any further collaboration will be done there.

@nikhilmufc7
Copy link

I made a gist with all the auth errors from firebase with an example https://gist.github.com/nikhilmufc7/6b74a3c12a6e2d3284942d40ff583e37

@lock
Copy link

lock bot commented Apr 15, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@lock lock bot locked and limited conversation to collaborators Apr 15, 2020
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p: firebase Firebase plugins package flutter/packages repository. See also p: labels. waiting for PR to land (fixed) A fix is in flight
Projects
None yet
Development

Successfully merging a pull request may close this issue.