2017-04-13 73 views
0

我正在嘗試設置一種方法,允許用戶使用Facebook登錄到他們的Firebase帳戶。如果這是第一次創建帳戶的用戶,一切正常,但如果用戶之前創建了帳戶並且正在嘗試登錄其帳戶,那麼問題就開始了。當代碼繼續在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

沒有必要創建一個帶有憑據登錄時的用戶代幣 – Callam

+0

我正在使用創建用戶來檢查用戶是否曾經創建過帳戶。所以,如果出現錯誤,它表明用戶已經有一個帳戶@Callam – notJenny

回答

1

一旦你創建的用戶,所有你需要的是登錄步驟,

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

即使它是Facebook用戶登錄,我可以做到這一點嗎? – notJenny

+0

您正在使用Facebook憑據在Firebase中創建新用戶 - 每次登錄時都不需要添加新用戶。我認爲,更好地轉換您的邏輯 - 嘗試登錄,如果失敗,請考慮創建一個帳戶。用戶只會是新的,但會多次返回 – Russell