简体   繁体   中英

Configure Firebase Authentication to send INVALID_CREDENTIALS instead of EMAIL_NOT_FOUND or INVALID_PASSWORD

I'm using free Firebase plan for testing my React apps and I'm working on authentication with email and password provider right now.

I'm using Firebase straight in my client.

I managed to handle authentication errors in UI and show users a message like Invalid credentials if either their email or password is wrong but I still get EMAIL_NOT_FOUND or INVALID_PASSWORD response from firebase in the network tab.

This is how I'm handling the UI

try {
  setFirebaseError("");
  await signInWithEmailAndPassword(auth, values.email, values.password);
} catch (err) {
  if (["auth/wrong-password", "auth/user-not-found"].includes(err.code)) setFirebaseError("Invalid credentials");
  else setFirebaseError(err.message);
}

But the response is either

{
  "error": {
    "code": 400,
    "message": "EMAIL_NOT_FOUND",
    "errors": [
      {
        "message": "EMAIL_NOT_FOUND",
        "domain": "global",
        "reason": "invalid"
      }
    ]
  }
}

or

{
  "error": {
    "code": 400,
    "message": "INVALID_PASSWORD",
    "errors": [
      {
        "message": "INVALID_PASSWORD",
        "domain": "global",
        "reason": "invalid"
      }
    ]
  }
}

and I don't want that.

Is there a way to configure Firebase Authenticaton to send custom error response?

Yes, you can do it

try {
  await FirebaseAuth.instance.signInWithEmailAndPassword(
  email: "your_given_email",
  password: "your_given_password!"
);
} 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);
            }
          }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM