Firebase AuthUI - 查找现有用户或新用户

时间:2018-04-07 23:15:34

标签: ios swift firebase firebase-authentication

我正在使用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
}

}

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您是否将用户保存到"用户"节点,带有Auth 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)
}