I have implemented email and Facebook login in my app using firebase. My code is as below:
For fb login :
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)
}
}
For email signup :
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.")
}
And helper class code:
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!
}
}
Problem is when i sign up with email user I can see that user data on dashboard of firebase and when i am sign up with fb , that user's data are not displaying on dashboard, let me know what is issue. Anything i miss? Its showing fb signup successfully
Firebase only stores a list of email+password users. It doesn't store any data for users of your app that are signed with social providers (such as Facebook). So there is nothing to show on the Firebase dashboard for those users.
If you want to store information about Facebook users in your database, see the section on storing user data.