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

🐛 [firebase_auth] Add proper errors handling #6687

Closed
rodion-m opened this issue Jul 23, 2021 · 1 comment
Closed

🐛 [firebase_auth] Add proper errors handling #6687

rodion-m opened this issue Jul 23, 2021 · 1 comment
Labels
plugin: auth type: enhancement New feature or request

Comments

@rodion-m
Copy link
Contributor

rodion-m commented Jul 23, 2021

At the moment error code stored as string and we even don't have exhaustive enum of possible errors.
It was not easy, but I found error codes list. Actually I found two:
https://cloud.google.com/identity-platform/docs/error-codes
https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signinwithcredential
So, I suggest this approach to error handling:

extension FirebaseAuthExceptionX on FirebaseAuthException {
  T handleErrors<T>({
    required T Function() defaultFallback,
    T Function()? accountExistsWithDifferentCredential,
    T Function()? missingPhoneNumber,
    T Function()? invalidPhoneNumber,
    T Function()? missingVerificationCode,
    T Function()? invalidVerificationCode,
    T Function()? missingVerificationId,
    T Function()? invalidVerificationId,
    T Function()? codeExpired,
    T Function()? invalidCredential,
    T Function()? userDisabled,
    T Function()? userNotFound,
    T Function()? operationNotAllowed,
    T Function()? captchaCheckFailed,
    T Function()? quotaExceeded,
    T Function()? wrongPassword,
    T Function()? unknown,
  }) {
    switch (code) {
      case FireBaseAuthErrorCode.accountExistsWithDifferentCredential:
        return (accountExistsWithDifferentCredential ?? defaultFallback).call();
      case FireBaseAuthErrorCode.missingPhoneNumber:
        return (missingPhoneNumber ?? defaultFallback).call();
      case FireBaseAuthErrorCode.invalidPhoneNumber:
        return (invalidPhoneNumber ?? defaultFallback).call();
      case FireBaseAuthErrorCode.missingVerificationCode:
        return (missingVerificationCode ?? defaultFallback).call();
      case FireBaseAuthErrorCode.invalidVerificationCode:
        return (invalidVerificationCode ?? defaultFallback).call();
      case FireBaseAuthErrorCode.missingVerificationId:
        return (missingVerificationId ?? defaultFallback).call();
      case FireBaseAuthErrorCode.invalidVerificationId:
        return (invalidVerificationId ?? defaultFallback).call();
      case FireBaseAuthErrorCode.codeExpired:
        return (codeExpired ?? defaultFallback).call();
      case FireBaseAuthErrorCode.invalidCredential:
        return (invalidCredential ?? defaultFallback).call();
      case FireBaseAuthErrorCode.userDisabled:
        return (userDisabled ?? defaultFallback).call();
      case FireBaseAuthErrorCode.userNotFound:
        return (userNotFound ?? defaultFallback).call();
      case FireBaseAuthErrorCode.operationNotAllowed:
        return (operationNotAllowed ?? defaultFallback).call();
      case FireBaseAuthErrorCode.captchaCheckFailed:
        return (captchaCheckFailed ?? defaultFallback).call();
      case FireBaseAuthErrorCode.quotaExceeded:
        return (quotaExceeded ?? defaultFallback).call();
      case FireBaseAuthErrorCode.wrongPassword:
        return (wrongPassword ?? defaultFallback).call();
      case FireBaseAuthErrorCode.missingClientIdentifier:
        return (missingClientIdentifier ?? defaultFallback).call();
      default:
        return (unknown ?? defaultFallback).call();
    }
  }
}
FireBaseAuthErrorCode
class FireBaseAuthErrorCode {

/// Thrown if there already exists an account with the email address asserted by the credential.
/// See: https://firebase.flutter.dev/docs/auth/error-handling/#handling-account-exists-with-different-credential-errors
static const accountExistsWithDifferentCredential = 'account-exists-with-different-credential';

/// Thrown if the SMS quota for the Firebase project has been exceeded.
static const quotaExceeded = 'quota-exceeded';

/// Thrown if the credential is malformed or has expired.
static const invalidCredential = 'invalid-credential';

/// 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.
static const operationNotAllowed = 'operation-not-allowed';

/// Thrown if the reCAPTCHA response token was invalid, expired, or if this method was called from a non-whitelisted domain.
static const captchaCheckFailed = 'captcha-check-failed';

static const missingPhoneNumber = 'missing-phone-number';
static const invalidPhoneNumber = 'invalid-phone-number';
static const missingVerificationCode = 'missing-verification-code';
static const invalidVerificationCode = 'invalid-verification-code';
static const missingVerificationId = 'missing-verification-id';
static const invalidVerificationId = 'invalid-verification-id';
static const codeExpired = 'code-expired';
static const userDisabled = 'user-disabled';
static const userNotFound = 'user-not-found';
static const wrongPassword = 'wrong-password';

}

Now we can use it like this:

try {
  final result = await FirebaseAuth.instance.signInWithCredential(credential);
  logger.info('Auth succeeded: $result');
} on FirebaseAuthException catch (e) {
  e.handleErrors(
	accountExistsWithDifferentCredential: () {
	  // todo: handle
	},
	invalidVerificationCode: () {
	  showError("Code is incorrect :(");
	},
	codeExpired: () {
	  showError("Code is expired :(");
	},
	defaultFallback: () {
	  showError('Error: ${e.code}');
	},
  );
}

It should be a part of API.
@Salakar @russellwheatley @rrousselGit Please review it and I'll make a PR.

@rodion-m rodion-m added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Jul 23, 2021
@rodion-m rodion-m changed the title 🐛 [firebase_auth] Add proper error codes handling 🐛 [firebase_auth] Add proper errors handling Jul 23, 2021
@markusaksli-nc markusaksli-nc added triage Issue is currently being triaged. plugin: auth type: enhancement New feature or request and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. type: bug Something isn't working labels Jul 23, 2021
@Lyokone
Copy link
Contributor

Lyokone commented Sep 28, 2022

Hello,
Modifying the errors API is already discussed here: #3273
I'll close this one in favor of the other

@Lyokone Lyokone closed this as completed Sep 28, 2022
@firebase firebase locked and limited conversation to collaborators Oct 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
plugin: auth type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants
@Lyokone @rodion-m @markusaksli-nc and others