我使用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()
}
}
谢谢。
答案 0 :(得分:3)
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项目的一部分,也可以 用于识别项目中每个应用程序的用户,无论如何 用户如何登录。
正如我所提到的,这是非常伪代码,但为解决方案提供了可能的顺序。