firebase - SwiftUI Firebase 如何进行自定义错误处理
问题描述
所以我正在创建一个使用 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()
}
}
解决方案
所有身份验证错误代码都列在身份验证文档中。
以下是如何处理错误并显示您自己的错误消息的快速片段。
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")
}
}
}
有很多方法可以对此进行编码,因此这只是一个示例。
推荐阅读
- python - 使用 sklearn 波士顿住房数据集:尝试为系数创建数据框
- html - 想让带边框的箭头响应
- usb - 无法看到从 ubuntu 复制到 USB 驱动器的文件到 USB 驱动器 ini windows
- angular - Angular 服务依赖注入
- rust - 为什么我不能使用`&Iterator
- ` 作为迭代器?
- javascript - In Typescript, is there any way to write a class as a Array, so I can do class[i], like a List
in C# - stm32 - 备份或读取核板 stm32 的设置
- javascript - 如何将确切的单词与长度匹配
- javascript - React Native设置状态对象数组的元素
- c++ - 生产者消费者线程同步