I have followed some tutorial but no one seams to work I need to get AuthErrorCode for firebase auth and Firestone for make the localizable this is my code
this is the login function where I need to call the errorHandlingFunction
Auth.auth().signIn(withEmail: emailTextField, password: passwordTextField) {result, error in
if error != nil {
self.alertMessage = self.errorHandling(error: error! as NSError)
self.showAlert.toggle()
self.isLoading = false
return
}
func errorHandling(error: NSError) -> String {
if let err = error as NSError?, let code = AuthErrorCode(rawValue: error.code)
{
switch code {
case .accountExistsWithDifferentCredential:
return String(localized: "Account already exist with different credetial", table: "Localization", comment: "alert message")
case .credentialAlreadyInUse:
return String(localized: "Credential are already in use", table: "Localization", comment: "alert message")
case .unverifiedEmail:
return String(localized: "An email link was sent to your account, please verify it before loggin in", table: "Localization", comment: "alert message")
case .userDisabled:
return String(localized: "User is currently disabled", table: "Localization", comment: "alert message")
case .userNotFound:
return String(localized: "Canno't find the user, try with different credential", table: "Localization", comment: "alert message")
case .weakPassword:
return String(localized: "Password is too weak", table: "Localization", comment: "alert message")
case .networkError:
return String(localized: "Error in network connection", table: "Localization", comment: "alert message")
case .wrongPassword:
return String(localized: "Password is wrong", table: "Localization", comment: "alert message")
case .invalidEmail:
return String(localized: "Email is not valid", table: "Localization", comment: "alert message")
default:
return String(localized: "Unknown error occurred", table: "Localization", comment: "alert message")
}
}
}
but I get this error from compiler
Cannot convert value of type 'Int' to expected argument type 'AuthErrorCode.Code'
is there a solution? and also for Firestone?
thanks
CodePudding user response:
To initialize a code you should go with
let nsError = error as NSError
AuthErrorCode.Code.init(rawValue: nsError.code)
CodePudding user response:
The issue is likely due to a function wanting a Code
instead of an Int
, according to the error. Let's simplify to handle all cases:
Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
if let maybeError = error { //if there was an error, handle it
let err = maybeError as NSError
switch err.code {
case AuthErrorCode.wrongPassword.rawValue:
print("wrong password")
case AuthErrorCode.invalidEmail.rawValue:
print("invalid email")
case AuthErrorCode.accountExistsWithDifferentCredential.rawValue:
print("accountExistsWithDifferentCredential")
... add the rest of the case statements
default:
print("unknown error: \(err.localizedDescription)")
}
} else { //there was no error so the user could be auth'd or maybe not!
if let _ = auth?.user {
print("user is authd")
} else {
print("no authd user")
}
}
})