簡體   English   中英

使用Firebase登錄Facebook-Swift iOS

[英]Facebook login using Firebase - Swift iOS

我正在使用Firebase實現與Facebook的登錄,我有此代碼,可在成功進行Facebook身份驗證后在數據庫中查找電子郵件(如果數據庫中存在)並在應用程序中登錄(如果找到)登錄我的數據庫,如果沒有,我想將用戶定向到注冊視圖控制器找到了,但由於此方法是異步的,因此無法正常工作。 如果有人可以提供幫助,我將不勝感激。 這是我的代碼:

  func getFacebookUserInfo() {
    if(FBSDKAccessToken.current() != nil){
        let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "id,name,gender,email,education"])
        let connection = FBSDKGraphRequestConnection()
        connection.add(graphRequest, completionHandler: { (connection, result, error) -> Void in
            let data = result as! [String : AnyObject]
            let email = data["email"] as? String

            let emailRef = FIRDatabase.database().reference().child("usernameEmailLink")
            emailRef.queryOrderedByValue().observe(.childAdded, with: { snapshot in
                if let snapshotValue = snapshot.value as? [String: AnyObject] {
                    for (key, value) in snapshotValue {

                        if(value as? String == email){
                            self.stringMode = snapshotValue["mode"]! as! String
                            self.username = key
                            self.parseUserInfoFromJSON()
                            return
                        }

                    }
                }

            })


        })
        connection.start()

    }

} 

謝謝。

用戶在Firebase中的注冊/存在可能應該在問題中的graphRequest代碼之前確定。

最重要的是(並且這很關鍵),電子郵件地址是動態的,因此不應將其用於驗證用戶是否存在。 例如,電子郵件地址為“ leroy@gmail.com”的用戶將其電子郵件更新為“ leroy.j@gmail.com”。 如果使用電子郵件來驗證注冊,則如果該電子郵件發生更改,它可能會完全中斷。

為此,請使用Firebase uid,因為它們是靜態且唯一的。

由於我們只有一小段代碼,因此我們不知道所使用的確切序列。 該答案是偽代碼,用於概述可能的序列。

我們假設“已注冊”意味着用戶已經過某種應用程序注冊序列,並且該用戶已在Firebase中創建(現在已經存在/已注冊)。

通常,會有一個登錄按鈕和一個委托方法來處理實際的登錄操作。

用戶輸入他們的登錄名,然后點擊“登錄”按鈕

func loginButton(loginButton: FBSDKLoginButton!, 
                 didCompleteWithResult result: FBSDKLoginManagerLoginResult!,
                 error: NSError?) {

然后,Firebase可以獲取該用戶的憑據(請參閱下面的Firebase文檔報價)

let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)

此時,登錄用戶並檢查他們是否在Firebase用戶節點中注冊(存在)。

FIRAuth.auth()?.signIn(with: credential) { (user, error) in
  if let error = error { //failed due to an error
    return
  }

  let uid = user.uid //the firebase uid
  let thisUserRef = userRef.child(uid) //a reference to the user node

  //check to see if the user exists in firebase (i.e. is Registered)
  thisUserRef.observeSingleEvent(of: .value, with: { (snapshot) in

    //if snapshot exists
        //then the user is already 'registered' in the user node
        //  so continue the app with a registered user
    //if not, then need to have the user go through a registration sequence and
    //  then create the user (make them registered) in the user node
        doRegisterUser(user)
  })


func doRegisterUser(user: FIRUser) {

  //get what you need from the user to register them
  // and write it to the users node. This could be from additional
  // questions or from their Facebook graph, as in the code in the
  // question

  //for this example, we'll just write their email address
  let email = user.email
  let dict = ["email": email]

  //create a child node in the users node with a parent of uid
  // and a child of email: their email
  thisUserRef.setValue(node)

  //next time the user logs in via FB authentication, their user node
  //  will be found as they are now a 'registered' user
}

從Firebase文檔

用戶首次登錄后,將創建一個新的用戶帳戶並將其鏈接到用戶登錄所用的憑據(即用戶名和密碼或身份驗證提供者信息)。 此新帳戶存儲為Firebase項目的一部分,無論用戶如何登錄,都可以用來在項目中的每個應用程序中識別用戶。

正如我所提到的,這是非常偽的代碼,但是為解決方案提供了可能的順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM