所以我正在創建一個使用 Firebase 作為后端的應用程式,我想顯示特定于用戶的自定義錯誤訊息而不是內置的 firebase 錯誤訊息。我該怎么做?
func signIn(withEmail email: String, password: String){
Auth.auth().signIn(withEmail: email, password: password) { (result,err) in
if let err = err {
print("DEBUG: Failed to login: \(err.localizedDescription)")
return
}
self.userSession = result?.user
self.fetchUser()
}
}
uj5u.com熱心網友回復:
身份驗證檔案中列出了所有身份驗證錯誤代碼。
這是如何處理錯誤并顯示您自己的錯誤訊息的快速片段。
Auth.auth().signIn....() { (auth, error) in //some signIn function
if let x = error {
let err = x as NSError
switch err.code {
case AuthErrorCode.wrongPassword.rawValue:
print("wrong password, you big dummy")
case AuthErrorCode.invalidEmail.rawValue:
print("invalid email - duh")
case AuthErrorCode.accountExistsWithDifferentCredential.rawValue:
print("the account already exists")
default:
print("unknown error: \(err.localizedDescription)")
}
} else {
if let _ = auth?.user {
print("authd")
} else {
print("no authd user")
}
}
}
有很多方法可以對此進行編碼,因此這只是一個示例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359425.html
