Reputation: 35659
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:
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.
Upvotes: 5
Views: 2538
Reputation: 12877
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) } } }
Upvotes: 10