我正在开发一个 iOS 应用程序,它将使用 Firebase 进行用户管理(注册、登录等)
我是 Firebase 的新手,但基本上一切正常。我已经连接了它,我已经创建了用户并登录了等等。
但是,我正在尝试更改我的 UI,以便“注册”按钮最初是隐藏的,并且仅在以下情况下出现:
- 所有字段都不为空
- 电子邮件地址有效(使用正则表达式)
- 电子邮件地址不在数据库中
- 用户名不在数据库中
- 密码和确认密码字段相等
我不知道#3 和#4。
我一直在阅读文档、观看视频、在 StackO 和其他地方寻找链接,但我无法弄明白。
谁能指出我正确的方向?
最佳答案
如果您使用电子邮件和密码身份验证,解决方案非常简单。
Firebase 身份验证不允许重复的电子邮件,因此当执行 createUser 函数时,如果电子邮件已经存在,Firebase 将返回 emailAlreadyInUse。错误参数中的错误。然后,您可以将其转换为 NSError 以查看它是哪一个并进行适当处理。
所以函数是这样的
Auth.auth().createUser(withEmail: createEmail, password: password ) { user, error in
if let x = error {
let err = x 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")
case AuthErrorCode.emailAlreadyInUse.rawValue: //<- Your Error
print("email is alreay in use")
default:
print("unknown error: \(err.localizedDescription)")
}
//return
} else {
//continue to app
}
我在 case 语句中加入了一些随机错误,但检查链接以获取所有 AuthErrorCodes 的完整列表。
你也可以这样做
Auth.auth().fetchSignInMethods(forEmail: user, completion: { (signInMethods, error) in
print(signInMethods)
})
关于ios - Firebase 身份验证和 Swift : Check if email already in database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56806437/