Reputation: 1603
We have a huge update on Firebase and I struggle with auth system. I want to show alerts when a different error is met.
For now I have
FIRAuth.auth()?.createUserWithEmail(email, password: pwd) { (user, error) in
if let error = error {
print(error.localizedDescription)
if error.code == statusTooShortPassword {
Alert(title: "Error", message: "The password must be 6 characters long or more")
.showOkay()
} else if error.code == statusEmailInvalid {
Alert(title: "Error", message: "Invalid email. Try again")
.showOkay()
} else {
print(error)
Alert(title: "Error", message: "Try again")
.showOkay()
}
}
}
But I have a strong feeling error.code
isn't the best way to handle new user creation. I am looking at this part of the docs:
But I don't know how to implement FIRAuthErrorCodeInvalidEmail
within FIRAuth.auth()?.createUserWithEmail
Can somebody please provide the right way of doing this, please?
UPDATE: When email is in invalid format I get this:
Error Domain=FIRAuthErrorDomain Code=17999 "An internal error has occurred, print and inspect the error details for more information." UserInfo={NSUnderlyingError=0x7fc3537ceca0 {Error Domain=FIRAuthInternalErrorDomain Code=3 "(null)" UserInfo={FIRAuthErrorUserInfoDeserializedResponseKey=<CFBasicHash 0x7fc3537d28c0 [0x1091b5a40]>{type = immutable dict, count = 3,
entries =>
0 : <CFString 0x7fc3537c8c80 [0x1091b5a40]>{contents = "message"} = <CFString 0x7fc3537d2740 [0x1091b5a40]>{contents = "INVALID_EMAIL"}
1 : errors = <CFArray 0x7fc3537ba570 [0x1091b5a40]>{type = immutable, count = 1, values = (
0 : <CFBasicHash 0x7fc3537a9190 [0x1091b5a40]>{type = immutable dict, count = 3,
entries =>
0 : reason = invalid
1 : message = <CFString 0x7fc3537e7030 [0x1091b5a40]>{contents = "INVALID_EMAIL"}
2 : domain = global
}
)}
2 : code = <CFNumber 0xb000000000001903 [0x1091b5a40]>{value = +400, type = kCFNumberSInt64Type}
}
}}, error_name=ERROR_INTERNAL_ERROR, NSLocalizedDescription=An internal error has occurred, print and inspect the error details for more information.}
My updated code:
if error.code == FIRAuthErrorCode.ErrorCodeInvalidEmail.rawValue {
Alert(title: "Error", message: "That is some funky email there. Try again")
.showOkay()
} else if error.code == FIRAuthErrorCode.ErrorCodeWeakPassword.rawValue {
Alert(title: "Error", message: "Password is too weak. Try 6 or more characters. Try again")
.showOkay()
} else {
print(error)
}
Upvotes: 1
Views: 1705
Reputation: 2182
I updated the answer to Swift 3 syntax according to @user2462805
Try this
FIRAuth.auth()?.createUser(withEmail: email, password: pwd) { (authResult, error) in
if let error = error {
if error._code == AuthErrorCode.invalidEmail.rawValue {
//the error code goes here
}
}
}
Upvotes: 3