Agung
Agung

Reputation: 13843

what are the error codes for Flutter Firebase Auth Exception?

I have tried to read this thread List of Authorisation errors with Firebase login , and also I have tried to search, but I just can find admin SDK authentication error in here here

those error code from those links are different from the error code for Firebase Auth for Flutter app

I mean, I need the error codes in here

Future<void> signInUsingEmail({required String email, required String password}) async {
    try {
      await _auth.signInWithEmailAndPassword(email: email, password: password);
    } on FirebaseAuthException catch (error) {

      // I need the error.code in here

      print(error.code);
      

    }

could I know all available error codes? so I can write my own error message in my own language. as for now, I can only catch these error codes below

what else?

Upvotes: 18

Views: 28723

Answers (5)

M A R S
M A R S

Reputation: 11

Well recently while building my flutter app, I came to realise that the only error code i was getting is 'invalid-credential' You can find out what error code you are recieving by printing them on to the console

try {
      await FirebaseAuth.instance.signInWithEmailAndPassword(
          email: emailController.text, password: passwordController.text);;
    } on FirebaseAuthException catch (e) {
      Navigator.pop(context);
      print('error: ${e.code}');
      if (e.code == 'invalid-credential') {
        print('Wrong credentials');
      }
    }

Upvotes: 1

Renaud Tarnec
Renaud Tarnec

Reputation: 83163

For the signInWithEmailAndPassword method for Flutter, you will find the error codes in the firebase_auth package documentation.

They are actually the same as the JS SDK ones: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#error-codes_12 (Link to document on Web Archive)

Upvotes: 15

Code on the Rocks
Code on the Rocks

Reputation: 17764

For anyone that does not like clicking on links:

signInWithEmailAndPassword

  • wrong-password: Thrown if the password is invalid for the given email, or the account corresponding to the email doesn't have a password set.
  • invalid-email: Thrown if the email address is not valid.
  • user-disabled: Thrown if the user corresponding to the given email has been disabled.
  • user-not-found: Thrown if there is no user corresponding to the given email.

createUserWithEmailAndPassword

  • email-already-in-use: Thrown if there already exists an account with the given email address.
  • invalid-email: Thrown if the email address is not valid.
  • operation-not-allowed: Thrown if email/password accounts are not enabled. Enable email/password accounts in the Firebase Console, under the Auth tab.
  • weak-password: Thrown if the password is not strong enough.

signInWithCredential

  • account-exists-with-different-credential: Thrown if there already exists an account with the email address asserted by the credential. Resolve this by calling fetchSignInMethodsForEmail and then asking the user to sign in using one of the returned providers. Once the user is signed in, the original credential can be linked to the user with linkWithCredential.
  • invalid-credential: Thrown if the credential is malformed or has expired.
  • operation-not-allowed: Thrown if the type of account corresponding to the credential is not enabled. Enable the account type in the Firebase Console, under the Auth tab.
  • user-disabled: Thrown if the user corresponding to the given credential has been disabled.
  • user-not-found: Thrown if signing in with a credential from EmailAuthProvider.credential and there is no user corresponding to the given email.
  • wrong-password: Thrown if signing in with a credential from EmailAuthProvider.credential and the password is invalid for the given email, or if the account corresponding to the email does not have a password set.
  • invalid-verification-code: Thrown if the credential is a PhoneAuthProvider.credential and the verification code of the credential is not valid.
  • invalid-verification-id: Thrown if the credential is a PhoneAuthProvider.credential and the verification ID of the credential is not valid.id.

reauthenticateWithCredential

  • user-mismatch: Thrown if the credential given does not correspond to the user.
  • user-not-found: Thrown if the credential given does not correspond to any existing user.
  • invalid-credential: Thrown if the provider's credential is not valid. This can happen if it has already expired when calling link, or if it used invalid token(s). See the Firebase documentation for your provider, and make sure you pass in the correct parameters to the credential method.
  • invalid-email: Thrown if the email used in a EmailAuthProvider.credential is invalid.
  • wrong-password: Thrown if the password used in a EmailAuthProvider.credential is not correct or when the user associated with the email does not have a password.
  • invalid-verification-code: Thrown if the credential is a PhoneAuthProvider.credential and the verification code of the credential is not valid.
  • invalid-verification-id: Thrown if the credential is a PhoneAuthProvider.credential and the verification ID of the credential is not valid.

signInWithAuthProvider

  • user-disabled: Thrown if the user corresponding to the given email has been disabled.

signInAnonymously

  • operation-not-allowed: Thrown if anonymous accounts are not enabled. Enable anonymous accounts in the Firebase Console, under the Auth tab.

signInWithEmailLink

  • expired-action-code: Thrown if OTP in email link expires.
  • invalid-email: Thrown if the email address is not valid.
  • user-disabled: Thrown if the user corresponding to the given email has been disabled.

Upvotes: 51

TAYAB FAROOQ
TAYAB FAROOQ

Reputation: 31

TextButton(
            onPressed: () async {
              try {
                //final user = FirebaseAuth.instance.currentUser;
                final email = _emailController.text;
                final password = _passwordController.text;
                // await FirebaseAuth.instance //   final userCredential =
                //     .signInWithEmailAndPassword(
                //         email: email, password: password);
                AuthService.firebase()
                    .logIn(email: email, password: password);
                final user = AuthService.firebase().currentUser;
                if (user?.isEmailVerified ?? false) {
                  Navigator.of(context).pushNamedAndRemoveUntil(
                      emailRoute, (route) => false);
                } else {
                  Navigator.of(context)
                      .pushNamedAndRemoveUntil(noteRoute, (route) => false);
                }

                //devtools.log(userCredential.toString());
                //This line of text may not work to print data
                // print(userCredential.toString());
              } on FirebaseAuthException catch (e) {
                if (e.code == 'network-request-failed') {
                  showErrorDialog(context, 'No Internet Connection');
                  //devtools.log('No Internet Connection');
                } else if (e.code == "wrong-password") {
                  return showErrorDialog(
                      context, 'Please Enter correct password');
                  //devtools.log('Please Enter correct password');
                  //print('Please Enter correct password');
                } else if (e.code == 'user-not-found') {
                  showErrorDialog(context, 'Email not found');
                  // print('Email not found');
                }  else if (e.code == 'too-many-requests') {
                  return showErrorDialog(
                      context, 'Too many attempts please try later');
                  //print('Too many attempts please try later');
                } else if (e.code == 'unknwon') {
                  showErrorDialog(
                      context, 'Email and password field are required');
                  //print('Email and password field are required');
                } else if (e.code == 'unknown') {
                  showErrorDialog(
                      context, 'Email and Password Fields are required');
                  //print(e.code);
                } else {
                  print(e.code);
                }
              }
            },
            child: const Text('Login')),

Upvotes: 0

Denzel
Denzel

Reputation: 1109

So I created this gist And it's basically a handler for some common firebase auth exceptions.

This is an example on how to use it

Future createUser(String email, String password) async {
    try {
      UserCredential customUserCredential = await FirebaseAuth.instance
          .createUserWithEmailAndPassword(
              email: email, password: password);
      return customUserCredential.user!.uid;
    } on FirebaseAuthException catch (authError) {
      throw CustomAuthException(authError.code, authError.message!);
    } catch (e) {
      throw CustomException(errorMessage: "Unknown Error");
    }

Upvotes: 1

Related Questions