swift 中的 firebase auth 和 Firestone 错误代码

问题描述 投票:0回答:4

我已经遵循了一些教程,但没有人接缝工作我需要获取 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'

有解决办法吗?还有凡士通?

谢谢

swift firebase google-cloud-firestore error-handling
4个回答
11
投票
根据错误,该问题可能是由于函数需要

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") } } })
    

4
投票
要初始化您应该使用的代码

let nsError = error as NSError AuthErrorCode.Code.init(rawValue: nsError.code)
    

3
投票
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 } }
    

0
投票
首先,为自定义错误消息定义一个扩展函数,如下所示 -

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)") } }
    
© www.soinside.com 2019 - 2024. All rights reserved.