ios - 在 Firebase 中不断收到 'existing email' 错误

标签 ios swift facebook firebase firebase-authentication

我正在尝试设置一种允许用户使用 Facebook 登录其 Firebase 帐户的方法。如果这是第一次创建帐户的用户,一切正常,但如果用户之前创建过帐户并试图登录到他们的帐户,那么问题就开始了。当代码

FIRAuth.auth()?.signIn(with: credential, completion: {(user, error) 

运行时我收到错误消息,指出该电子邮件已在使用中,即使这是他们尝试登录的帐户。

我的全部代码在这里:

func handleCustomFBLogin() {
    FBSDKLoginManager().logIn(withReadPermissions: ["email"], from: self) { (result, err) in
        if err != nil {
            print("Error loggin in is \(err)")
            //self.facebookanimateIn()
        } else if (result?.isCancelled)!{
            print("The user cancelled loggin in ")
        } else {
            let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
                            let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email"])
            graphRequest.start(completionHandler: { (connection, result, error) -> Void in
                if ((error) != nil) {
                    print("Error: \(error)")
                } else {

                    let data:[String:AnyObject] = result as! [String: AnyObject]
                    let facebookName:NSString =  data["name"] as! NSString

                    let facebookEmail = data["email"] as Any

                    let userId = data["id"] as! NSString
                    let facebookProfileUrl = "http://graph.facebook.com/\(userId)/picture?type=large"
                    let facebookAge = data["age_range"] as Any
                    let password = "needToPutRandomizedPasswordInHere" as String
                    FIRAuth.auth()?.createUser(withEmail: facebookEmail as! String, password: password, completion: {result, error in
                        if error != nil{
                            //user has account, they just need to sign in
                            FIRAuth.auth()?.signIn(with: credential, completion: {(user, error) in
                                if error != nil{
                                    print(error.debugDescription)
                                    return
                                }

                                let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
                                let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "checker") as! UINavigationController
                                self.present(vc, animated: true, completion: nil)
                               // ref.removeObserver(withHandle: handle)
                            })
                        } else {
                            //user does not have an account and they need to create one
                            guard let uid = result?.uid else{
                                return
                            }
                            print("user created as \(uid)")
                            let val = "0"
                            let number = (val as NSString).integerValue
                            let ref = FIRDatabase.database().reference()
                            let usersReference = ref.child("Users").child(uid)
                            let values = [""] as [String : Any];
                            usersReference.updateChildValues(values, withCompletionBlock: { (err,ref) in
                                if err != nil {
                                    print(err.debugDescription)
                                    return}})
                            print("Save the user successfully into Firebase database")
                            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
                            let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "checker") as! UINavigationController
                            self.present(vc, animated: true, completion: nil)


                        }
                    })
                }
            })
        }
    }
}

最佳答案

创建用户后,只需登录即可

 FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: { (user, error) in
        if error != nil {
            // handle errors
        } else {
            // continue with login process
        }
    })

关于ios - 在 Firebase 中不断收到 'existing email' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43385300/

相关文章:

关于点赞框的 Facebook 描述未显示在 Facebook 墙上

ios - 我怎样才能用来自底部而不是顶部的原点来设置 CALayer 的高度动画?

ios - NSMutableAttributedString斜体和粗体不能同时工作

ios - 如何在 Swift 中首先打开应用程序请求权限?

swift - 字典导致 Swift 中索引超出范围错误

ios - Tintcolor left back arrow navigationBar 在关闭 UIAlertController 后发生变化(IOS)

facebook - 如何为 Facebook 页面的粉丝获取(更好的)人口统计数据?

ios - FBSDKLoginManager 返回 "You have already authorized foobar"并且不重定向到我的 View

ios - 快速导航栏行为

ios - 使用plug man安装一个phone gap plugin iOs