【问题标题】:Firebase AuthUI - Find if existing user or new userFirebase AuthUI - 查找现有用户还是新用户
【发布时间】:2018-04-07 23:15:33
【问题描述】:

我正在使用 firebase AuthUI 来注册/登录用户。根据用户是新用户还是现有用户,我需要将它们重定向到不同的视图控制器。 firebase 是否提供检查功能?

这是我当前的代码,无论是新用户还是注册用户,都使用“signUpSegue”

@IBAction func phoneNumberLoginAction(_ sender: AnyObject) {
    FUIAuth.defaultAuthUI()?.delegate = self as FUIAuthDelegate
    let phoneProvider = FUIPhoneAuth.init(authUI: FUIAuth.defaultAuthUI()!)
    FUIAuth.defaultAuthUI()?.providers = [phoneProvider]
    // 1
    print("clicked butto")
    guard let authUI = FUIAuth.defaultAuthUI()
        else { return }

    // 2
    authUI.delegate = self as FUIAuthDelegate

    // 3
    let authViewController = authUI.authViewController()
    present(authViewController, animated: true)
}

@available(iOS 10.0, *)
extension WelcomeViewController: WelcomePageViewControllerDelegate, FUIAuthDelegate {
func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?) {
    print("handle user signup / login")

    performSegue(withIdentifier: "signUpSegue", sender: nil)
}

func welcomePageViewController(_ welcomePageViewController: WelcomePageViewController,
                                didUpdatePageCount count: Int) {
    pageControl.numberOfPages = count
}

func welcomePageViewController(_ welcomePageViewController: WelcomePageViewController,
                                didUpdatePageIndex index: Int) {
    pageControl.currentPage = index
}

}

【问题讨论】:

    标签: ios swift firebase firebase-authentication


    【解决方案1】:

    没有记录,但有一个回调函数didSignInWithAuthDataResult,它返回一个AuthDataResult,它提供了一种通过additionalUserInfo.newUser判断用户是新用户还是现有用户的方法。

    https://github.com/firebase/FirebaseUI-iOS/blob/a69aa9536a0f5312bdd2d408a761c7dd21698015/FirebaseAuthUI/FUIAuth.h#L54

    【讨论】:

      【解决方案2】:

      您是否将用户保存到“用户”节点下的数据库中,以及他们的身份验证 ID?如果是,只需使用返回的 UID 进行查询,看看它们是否像这样存在......

      func checkIfUserExistsAlready(_ uid: String, complete: @escaping ((_ doesExist: Bool) -> Void)) {
      
       //userRef if a Database Reference to "Users" node
      
              userRef.child(uid).observeSingleEvent(of: .value) { (snapshot) in
                  if snapshot.exists() {
                      complete(true)
                  } else {
                      complete(false)
                  }
              }
          }
      

      您可以调用它并签入 FUIAuthDelegate 函数

       func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?) {
      
          //Call does exists function here with user.uid
      
          //Have if statement here and perform segues accordingly
      
          performSegue(withIdentifier: "signUpSegue", sender: nil)
      }
      

      【讨论】:

        【解决方案3】:

        a) 尝试使用电话号码登录 Firebase。 b) 如果 Firebase 返回错误,请检查 error?.localizedDescription c) 如果错误是不存在的用户,那么您的用户是新用户。 d) 如果错误是密码错误,请尝试其他密码。

        【讨论】:

          猜你喜欢
          • 2019-08-17
          • 2019-01-05
          • 2021-02-13
          • 2020-07-20
          • 2018-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-05
          相关资源
          最近更新 更多