我正在尝试设置一种方法,允许用户使用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)
}
})
}
})
}
}
}
答案 0 :(得分:1)
创建用户后,只需登录步骤
即可 FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
// handle errors
} else {
// continue with login process
}
})