我已使用firebase在我的应用中实施了电子邮件和Facebook登录。我的代码如下:
对于fb登录:
FireBaseHelper.fireBaseHelper.BASE_REF.authWithOAuthProvider("facebook", token: "123") { (error, data) -> Void in
APP_DELEGATE.hideActivityIndicator()
if error != nil {
print(error)
self.loginErrorAlert("Oops!", message: "Check your username and password.")
} else {
// Be sure the correct uid is stored.
NSUserDefaults.standardUserDefaults().setValue(data.uid, forKey: "uid")
// Enter the app!
self.performSegueWithIdentifier("CurrentlyLoggedIn", sender: nil)
}
}
对于电子邮件注册:
let username = usernameField.text
let email = emailField.text
let password = passwordField.text
if username != "" && email != "" && password != "" {
APP_DELEGATE.showActivityIndicator()
FireBaseHelper.fireBaseHelper.BASE_REF.createUser(email, password: password, withValueCompletionBlock: { error, result in
if error != nil {
APP_DELEGATE.hideActivityIndicator()
// There was a problem.
self.signupErrorAlert("Oops!", message: "Having some trouble creating your account. Try again.")
} else {
// Create and Login the New User with authUser
FireBaseHelper.fireBaseHelper.BASE_REF.authUser(email, password: password, withCompletionBlock: {
err, authData in
let user = ["provider": authData.provider!, "email": email!, "username": username!]
FireBaseHelper.fireBaseHelper.USER_REF.childByAppendingPath(authData.uid).setValue(user, withCompletionBlock: { (error, firebase) -> Void in
if error != nil {
APP_DELEGATE.hideActivityIndicator()
self.signupErrorAlert("Oops!", message: "Having some trouble creating your account. Try again.")
}else
{
APP_DELEGATE.hideActivityIndicator()
// Store the uid for future access - handy!
NSUserDefaults.standardUserDefaults().setValue(result ["uid"], forKey: "uid")
// Enter the app.
self.performSegueWithIdentifier("NewUserLoggedIn", sender: nil)
}
})
})
}
})
}else
{
signupErrorAlert("Oops!", message: "Don't forget to enter your email, password, and a username.")
}
辅助类代码:
class FireBaseHelper: NSObject {
static let fireBaseHelper = FireBaseHelper()
private var _BASE_REF = Firebase(url: "https://poc-demoApp.firebaseio.com")
private var _USER_REF = Firebase(url: "https://poc-demoApp.firebaseio.com/users")
var BASE_REF: Firebase {
return _BASE_REF
}
var USER_REF: Firebase {
return _USER_REF
}
var CURRENT_USER_REF: Firebase {
let userID = NSUserDefaults.standardUserDefaults().valueForKey("uid") as! String
let currentUser = Firebase(url: "\(BASE_REF)").childByAppendingPath("users").childByAppendingPath(userID)
return currentUser!
}
}
问题是,当我使用电子邮件用户注册时,我可以在firebase的仪表板上看到用户数据,当我注册fb时,该用户的数据未显示在仪表板上,让我知道问题所在。我想念的一切?它成功显示fb注册
答案 0 :(得分:4)
Firebase仅存储电子邮件+密码用户列表。它不会为使用社交提供商(例如Facebook)签名的应用用户存储任何数据。因此,Firebase信息中心无法显示这些用户。
如果您想在数据库中存储有关Facebook用户的信息,请参阅storing user data部分。
答案 1 :(得分:0)
我遇到了同样的问题,我发现用户成功登录Facebook后,您需要获取登录使用的访问令牌,将其换成Firebase凭据,最后使用Firebase进行身份验证Firebase凭证。
以下是一个示例(不要忘记导入FBSDKLoginKit
和FirebaseAuth
库):
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signIn(with: credential) { (user, error) in
if let error = error {
print("Facebook authentication with Firebase error: ", error)
return
}
print("User signed in!") // After this line the Facebook login should appear on your Firebase console
}
以下是Facebook登录后控制台的屏幕截图: