Hibernia Hibernia - 1 year ago 201
Swift Question

Checking for user authentication error codes with Swift 3

In older versions of Swift, the following code could be used to check for user auth errors:

if (error != nil) {
// an error occurred while attempting login
if let errorCode = FAuthenticationError(rawValue: error.code) {
switch (errorCode) {
case .UserDoesNotExist:
println("Handle invalid user")
case .InvalidEmail:
println("Handle invalid email")
case .InvalidPassword:
println("Handle invalid password")
println("Handle default situation")

doesn't seem to exist anymore, and the documentation makes it look like it's been replaced with

in where
is results in the error:

cannot call nonfunctiontype String

Here's the documentation I'm looking at : https://firebase.google.com/docs/auth/ios/errors

Any ideas how to achieve what was done with the first block of code in Swift 3?

Answer Source

Use FIRAuthErrorCode - it is an int enum

enum FIRAuthErrorCode { FIRAuthErrorCodeInvalidCustomToken = 17000, FIRAuthErrorCodeCustomTokenMismatch = 17002, FIRAuthErrorCodeInvalidCredential = 17004, FIRAuthErrorCodeUserDisabled = 17005,

From here: https://firebase.google.com/docs/reference/ios/firebaseauth/interface_f_i_r_auth_errors

Try to use like this:

if (error != nil) {
    // an error occurred while attempting login
    if let errCode = FIRAuthErrorCode(rawValue: (error?._code)!) {
                switch errCode {
                case .errorCodeEmailAlreadyInUse:
                case .errorCodeInvalidEmail:
                case .errorCodeWrongPassword:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download