我已经遵循了一些教程,但没有人接缝工作我需要获取 AuthErrorCode for firebase auth 和 Firestone 来进行本地化,这是我的代码
这是我需要调用 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")
}
}
}
但是我从编译器得到这个错误
Cannot convert value of type 'Int' to expected argument type 'AuthErrorCode.Code'
有解决办法吗?还有凡士通?
谢谢
Code
而不是
Int
造成的。让我们简化处理所有情况:
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")
}
}
})
let nsError = error as NSError
AuthErrorCode.Code.init(rawValue: nsError.code)
Auth.auth().signIn(withEmail: emailTextField, password: passwordTextField) {result, error in
if let error = error {
let err = error as NSError
if let authErrorCode = AuthErrorCode.Code(rawValue: err.code) {
switch authErrorCode {
case .wrongPassword:
print("wrong password")
case .invalidEmail:
print("invalid email")
// ... other cases
default:
print("unknown error")
}
}
return
}
}
import Foundation
import FirebaseAuth
extension NSError {
func getErrorMessage() -> String {
let code = AuthErrorCode.Code(rawValue: self.code)
if(code == .invalidEmail){
return "Invalid email"
}
else if(code == .invalidCredential){
return "Wrong email or password"
}
else if(code == .wrongPassword){
return "Invalid password"
}
else if(code == .networkError)
{
return "Network error"
}
else if(code == .weakPassword){
return "Weak Password"
}
else {
return "Unknown error"
}
}
}
现在像这样调用您的 Firebase 身份验证函数 -
func submitLoginForm(email:String, password:String) async-> Void{
do{
try await Auth.auth().signIn(withEmail: email, password: password)
}catch{
let err = error as NSError
let errorMessage = err.getErrorMessage()
print("Error: \(errorMessage)")
}
}