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.
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.
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.
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.
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) } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With