【问题标题】:How to check if email is already in use and if email is in bad format prior to creating the user via Firebase (Swift)如何在通过 Firebase (Swift) 创建用户之前检查电子邮件是否已被使用以及电子邮件格式是否错误
【发布时间】:2019-05-30 20:39:53
【问题描述】:

是否可以在拨打Auth.auth().createUser(withEmail: createEmail...之前检查一个电子邮件地址是否已被使用

我有多个注册视图控制器,用户必须输入电子邮件地址,创建用户名,然后输入他们的姓名并按确切顺序创建密码。然而,除了名称和密码之外,这些都是独立的视图控制器(一个是合二为一的)。

我希望用户知道输入的电子邮件地址的格式是否正确,或者它是否已被使用。我想出如何做到这一点的唯一方法是在用户浏览所有视图控制器然后单击检查所有错误的注册按钮之后。

这是我在完成所有步骤后创建用户的最终视图控制器的代码(按下注册按钮)

 Auth.auth().createUser(withEmail: createEmail as! String, password: password as! String ) { (user, error) in

        // stop activity indicator
        self.nextButton.setTitle("Sign Up", for: .normal)
        self.activityIndicator.stopAnimating()
        if error == nil && user != nil {
            print("User Created!")
            // If user is created go to Welcome Page
            self.performSegue(withIdentifier: "goToWelcomeVC", sender: self)
            } else {
        // If there is an error creating an account
            print("error creating User: \(error!.localizedDescription)")
            print(error!._code)
            self.handleError(error!)
            return

这是我正在使用的电子邮件视图控制器的代码。 (按下继续按钮)** 不介意 AuthErrorCode 的大部分内容,我试图找到一种方法来调用它并将其合并到错误检查中.. 对我来说似乎效果不佳。

// TODO check email
   let invalidEmail = AuthErrorCode.invalidEmail
   let emailTaken = AuthErrorCode.emailAlreadyInUse

    }

    if (emailTextField.text?.isEmpty)! {
        print("textField is empty")
        let alert = UIAlertController(title: "Oops!", message: "Please enter an email address.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: " Try Again", style: UIAlertAction.Style.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    } else {
        self.performSegue(withIdentifier: "goToCreateUsernameVC", sender: self)
    }

}

【问题讨论】:

    标签: swift firebase email error-handling firebase-authentication


    【解决方案1】:

    您需要查看fetchSignInMethodsForEmail。它提供了一个 API 来查找所提供电子邮件的所有相关登录方法。如果邮箱没有注册,你会得到一个空数组。

    【讨论】:

      猜你喜欢
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多