Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Firebase 5 Authentication Errors In Swift 4

We have an existing project which has been recently updated to Firebase 5, Swift 4 and our Authentication error handling doesn't appear to be working.

I found several answers here on SO, but those no longer appear to be applicable:

Error Handling

Auth Codes

Suppose a user is logging in and they enter a valid email and invalid password, which are passed to the following code to authenticate

Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
    if error != nil {
        let errDesc = error?.localizedDescription
        print(errDesc!) //prints 'The password is invalid'
        let err = error!
        let errCode = AuthErrorCode(rawValue: err._code)
        switch errCode {
        case .wrongPassword: //Enum case 'wrongPassword' not found in type 'AuthErrorCode?'
            print("wrong password")
        default:
            print("unknown error")
        }
    } else {
        print("succesfully authd")
    }
})

Previously, we could use FIRAuthErrorCode to compare to possible errors such as FIRAuthErrorCodeInvalidEmail, FIRAuthErrorCodeWrongPassword etc but the code posted above won't compile due to this the error on this line

case .wrongPassword:   Enum case 'wrongPassword' not found in type 'AuthErrorCode?'

oddly, if I use autofill by typing

case AuthErrorCode.wr

.wrongPassword is a selectable option and when seleted the compiler shows

Enum case 'wrongPassword' is not a member of type 'AuthErrorCode?'

even though it was a selectable option.

like image 546
Jay Avatar asked Jun 09 '18 14:06

Jay


People also ask

How do I handle Firebase errors?

Stay organized with collections Save and categorize content based on your preferences. The Firebase Authentication SDKs provide a simple way for catching the various errors which may occur which using authentication methods. The SDKs for Flutter expose these errors via the FirebaseAuthException class.

How do I get Firebase authentication error message?

Each user must have a unique email. The provided Firebase ID token is expired. The Firebase ID token has been revoked. The credential used to initialize the Admin SDK has insufficient permission to access the requested Authentication resource.

How does Firebase handle authentication?

Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.


1 Answers

You should handle error code by converting Error into NSError.

  Auth.auth().signIn(withEmail: email, password: password) { (authResult, error) in
    switch error {
    case .some(let error as NSError) where error.code == AuthErrorCode.wrongPassword.rawValue:
      print("wrong password")
    case .some(let error):
      print("Login error: \(error.localizedDescription)")
    case .none:
      if let user = authResult?.user {
        print(user.uid)
      }
    }
  }
like image 51
Ibrahim Ulukaya Avatar answered Sep 20 '22 11:09

Ibrahim Ulukaya