How to Handle Firebase Auth exceptions on flutter

前端 未结 12 1053
走了就别回头了
走了就别回头了 2020-11-30 07:56

Please does anyone know how to catch firebase Auth exceptions on flutter and display them?

Note: I am not interested in the console (catcherror((e) print(e))

相关标签:
12条回答
  • 2020-11-30 08:27

    I prefer to create api layer response and error models and wrap the firebase plugin error and response objects in them. For sign in with email and password i have this

    @override
      Future<dynamic> loginWithEmailAndPassword(String email, String password) async {
        try {
          await _firebaseAuth.signInWithEmailAndPassword(
              email: email, password: password);
          return FirebaseSignInWithEmailResponse();
        } catch (exception) {
          return _mapLoginWithEmailError(exception);
        }
      }
    
      ApiError _mapLoginWithEmailError(PlatformException error) {
        final code = error.code;
        if (code == 'ERROR_INVALID_EMAIL') {
          return FirebaseSignInWithEmailError(
              message: 'Your email is not valid. Please enter a valid email',
              type: FirebaseSignInWithEmailErrorType.INVALID_EMAIL);
        } else if (code == 'ERROR_WRONG_PASSWORD') {
          return FirebaseSignInWithEmailError(
              message: 'Your password is incorrect',
              type: FirebaseSignInWithEmailErrorType.WRONG_PASSWORD);
        } else if (code == 'ERROR_USER_NOT_FOUND') {
          return FirebaseSignInWithEmailError(
              message: 'You do not have an account. Please Sign Up to'
                  'proceed',
              type: FirebaseSignInWithEmailErrorType.USER_NOT_FOUND);
        } else if (code == 'ERROR_TOO_MANY_REQUESTS') {
          return FirebaseSignInWithEmailError(
              message: 'Did you forget your credentials? Reset your password',
              type: FirebaseSignInWithEmailErrorType.TOO_MANY_REQUESTS);
        } else if (code == 'ERROR_USER_DISABLED') {
          return FirebaseSignInWithEmailError(
              message: 'Your account has been disabled. Please contact support',
              type: FirebaseSignInWithEmailErrorType.USER_DISABLED);
        } else if (code == 'ERROR_OPERATION_NOT_ALLOWED') {
          throw 'Email and Password accounts are disabled. Enable them in the '
              'firebase console?';
        } else {
          return FirebaseSignInWithEmailError(
              message: 'Make sure you have a stable connection and try again'
              type: FirebaseSignInWithEmailErrorType.CONNECTIVITY);
        }
      }
    

    I never return the AuthResult from firebase. Instead i listen to the onAuthStateChanged stream and react accordingly if there is a change.

    0 讨论(0)
  • 2020-11-30 08:29

    I just coded myself a way to do this without Platform dependent Code:

    This is possible since .signInWithEmailAndPassword correctly throws Errors with defined codes, that we can grab to identify the error and handle things in the way the should be handled.

    The following example creates a new Future.error, if any error happens, and a Bloc is then configured to shovel that data through to the Widget.

    Future<String> signIn(String email, String password) async {
      FirebaseUser user;
      String errorMessage;
    
      try {
        AuthResult result = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
        user = result.user;
      } catch (error) {
        switch (error.code) {
          case "ERROR_INVALID_EMAIL":
            errorMessage = "Your email address appears to be malformed.";
            break;
          case "ERROR_WRONG_PASSWORD":
            errorMessage = "Your password is wrong.";
            break;
          case "ERROR_USER_NOT_FOUND":
            errorMessage = "User with this email doesn't exist.";
            break;
          case "ERROR_USER_DISABLED":
            errorMessage = "User with this email has been disabled.";
            break;
          case "ERROR_TOO_MANY_REQUESTS":
            errorMessage = "Too many requests. Try again later.";
            break;
          case "ERROR_OPERATION_NOT_ALLOWED":
            errorMessage = "Signing in with Email and Password is not enabled.";
            break;
          default:
            errorMessage = "An undefined Error happened.";
        }
      }
    
      if (errorMessage != null) {
        return Future.error(errorMessage);
      }
    
      return user.uid;
    }
    
    0 讨论(0)
  • 2020-11-30 08:32
       try {
      final newuser = await _auth.createUserWithEmailAndPassword(
          email: emailController.text, password: passwordController.text);
      // print("Done");
    } catch (e) {
      print(e);
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: new Text(e.message),
            actions: <Widget>[
              FlatButton(
                child: new Text("OK"),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        },
      );
    }
    
    0 讨论(0)
  • 2020-11-30 08:33

    the exceptions can be handled using, FirebaseAuthException class.

    Here's the code for login using email and password:

    void loginUser(String email, String password) async {
        try {
          await _auth.signInWithEmailAndPassword(email: email, password:password);
        } on FirebaseAuthException catch (e) {
          // Your logic for Firebase related exceptions
        } catch (e) {
        // your logic for other exceptions!
    }
    

    You can use your own logic to handle the error, e.g show a alert dialog, etc. Same can be done for creating a user.

    0 讨论(0)
  • 2020-11-30 08:33

    I have the same error of "firebse platform exeption:" in flutter using "Firebase auth" and it didn't resolve even using try catch and trim() method in passing arrguments.

    The problem is when you run app using Button "Run" in main.dart it won't callback and catch the error.

    Solution: In Vscode terminal type "Flutter run" (for debug mode). or "Flutter run --release" (for release mode) now you won't face platform exception.

    0 讨论(0)
  • 2020-11-30 08:36

    NEW ANSWER (18/09/2020)

    If you are using firebase_auth: ^0.18.0, error codes have changed!

    For instance: ERROR_USER_NOT_FOUND is now user-not-found

    I could not find any documentation about that, so I went into the source code and read comments for every error codes. (firebase_auth.dart)

    I don't use all error codes in my app (e.g verification, password reset...) but you will find the most common ones in this code snippet:

    (It handles old and new error codes)

    String getMessageFromErrorCode() {
        switch (this.errorCode) {
          case "ERROR_EMAIL_ALREADY_IN_USE":
          case "account-exists-with-different-credential":
          case "email-already-in-use":
            return "Email already used. Go to login page.";
            break;
          case "ERROR_WRONG_PASSWORD":
          case "wrong-password":
            return "Wrong email/password combination.";
            break;
          case "ERROR_USER_NOT_FOUND":
          case "user-not-found":
            return "No user found with this email.";
            break;
          case "ERROR_USER_DISABLED":
          case "user-disabled":
            return "User disabled.";
            break;
          case "ERROR_TOO_MANY_REQUESTS":
          case "operation-not-allowed":
            return "Too many requests to log into this account.";
            break;
          case "ERROR_OPERATION_NOT_ALLOWED":
          case "operation-not-allowed":
            return "Server error, please try again later.";
            break;
          case "ERROR_INVALID_EMAIL":
          case "invalid-email":
            return "Email address is invalid.";
            break;
          case "ERROR_USER_NOT_FOUND":
          case "user-not-found":
            return "No account found with this email";
            break;
          default:
            return "Login failed. Please try again.";
            break;
        }
      }
    
    0 讨论(0)
提交回复
热议问题