ios - Firebase 身份验证和 Swift : Check if email already in database

标签 ios swift firebase firebase-authentication

我正在开发一个 iOS 应用程序,它将使用 Firebase 进行用户管理(注册、登录等)

我是 Firebase 的新手,但基本上一切正常。我已经连接了它,我已经创建了用户并登录了等等。

但是,我正在尝试更改我的 UI,以便“注册”按钮最初是隐藏的,并且仅在以下情况下出现:

  1. 所有字段都不为空
  2. 电子邮件地址有效(使用正则表达式)
  3. 电子邮件地址不在数据库中
  4. 用户名不在数据库中
  5. 密码和确认密码字段相等

我不知道#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/

相关文章:

ios - 如何根据 dp(不是点数)在 iOS 中设置 UILabel 字体大小,就像只有自动布局的 android 一样

ios - 插入行后重新加载 tableView

ios - 删除 IQKeyboardManagerSwift 上的完成栏

ios - 将 Firebase 数据库的子级存储在数组中

android - 无法解析到快照 : Firebase setValue ANDROID

javascript - 使用 GWT 时找不到 Firebase Service worker(404 错误)

ios - react 导航重置操作不起作用

iPhone 核心数据对多关系和取消对子对象的编辑

ios - 导航 Controller 后退按钮标题显示上一个标题

node.js - 使用带有 GET 的查询字符串在本地测试 Firebase HTTP Cloud 函数