Handing Firebase + Facebook login process
When the user clicks on the FB Login button :
-> Do the native FB login . (like what you are doing now)
-> do the firebase authentication.
-> But additionally, check whether the Data exists in the FirDatabase
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "name"]).start { (connection, result, err) in
let accessToken = FBSDKAccessToken.current()
guard let accessTokenString = accessToken?.tokenString else {return}
let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString)
FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
if error != nil {
return
}
self.checkDataExistsinfirDataBaaseForUID(user.uid){
loginStaus in
if(loginStaus){
// logged in
self.performSegue(withIdentifier: "loginToRooms", sender: nil)
}else{
// not logged in
self.performSegue(withIdentifier: "showSetupScreen", sender: nil)
}
}
})
if err != nil {
return
}
}
Ideally, after the setup page you must be adding the set-up information in the FirebaseDatabase under the node users -> uID. So check whether any such node is present in this method.
func checkDataExistsinfirDataBaaseForUID(_ uid: String, completion: (result: Bool) -> Void) {
let ref = FIRDatabase.database().reference().child("users").child(uid)
ref.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
completion(snapshot.exists())
})
}
Test cases :
- If a brand new user taps the FB login button on the login screen, they'll be taken to the setup screen.
Fulfilled.
- If an existing, but logged out, user taps the FB login button, they'll bypass the setup screen and go to the first page in the app.
After fb login-> firbase authenticateion ->Firdatabse node check -> if Returns true->login to app
- If an existing user signs up then never logs out, they'll never see the login or signup screen again.
By using your process of cehcking, FIRAuth.auth()?.addStateDidChangeListener {}
Facebook Login Firebase
When a user signs in to an app with their Facebook login, they get a unique ID for that app from Facebook. If the user ever signs in to the same app with Facebook again, they get that same ID.
Firebase Facebook login button does not change after user log out
Just call logOut()
from FBSDKLoginManager
Logs the user out
This calls [FBSDKAccessToken setCurrentAccessToken:nil] and [FBSDKProfile setCurrentProfile:nil].
// my signout method
let firebaseAuth = FIRAuth.auth()
do {
try firebaseAuth?.signOut()
FBSDKLoginManager().logOut()
} catch let signOutError as NSError {
print ("Error signing out: %@", signOutError)
}
Don't forget to add import FBSDKLoginKit
at the class where you use it
How to know if user is signing up or signing in with Facebook & Firebase
If you are using Firebase's Database, I assume you are storing information about that user in there. When you authorize a user, check to see if that user is in your database. If they aren't, it is the first time they are signing up. Here is what I do:
let accessToken = FBSDKAccessToken.current()
guard let accessTokenString = accessToken?.tokenString else {return}
let credentials = FacebookAuthProvider.credential(withAccessToken: accessTokenString)
Auth.auth().signIn(with: credentials, completion: { (user, error) in
let id = Auth.auth().currentUser?.uid
let ref = Database.database().reference(withPath: "users")
ref.child(id).observeSingleEvent(of: .value, with: {(snapshot) in
if snapshot.exists() {
//User is signing IN
} else {
//User is signing UP
}
}
}
Assumed database structure:
- users
- uniqueUserId
How to send Facebook authentication details to Firebase using a Cordova plugin & Firebase template
(Just an answer to the last update, as you figured out the rest :))
How to get user email from CordovaFacebook.login()
Looking at the CordovaFacebook documentation you can add a permissions
property on the object passed to the login
method.
According to the Facebook API documentation the permission name for email is just "email"
.
I haven't tested, but I think this should work:
CordovaFacebook.login({
permissions: [ 'email' ],
onSuccess: function(result) {
console.log('email:', result.email);
...
},
onFailure: function(result) {
...
}
});
Facebook login with Firebase. Can't find OAuth redirect URI
If you have just created your Facebook app, then you have to first add the "Facebook Login" Product.
You can do so by clicking on the + Add Product
link under the PRODUCTS
section on the left side of the page and by choosing Facebook Login
in the resulting page.
Once you've added this Product you can find the settings in the following page
https://developers.facebook.com/sa/apps/YOUR_APP_ID/fb-login/
Please replace YOUR_APP_ID
with your real App ID, which is a string like 078717227531318
.
Related Topics
iOS Library to Convert Powerpoint Presentations into Images
Handing Firebase + Facebook Login Process
Parsing JSON Using the New Swift 3 and Alamofire
Autolayout: Uiview Within Uiview Has Incorrect Frame
Autolayout Ignores Multi-Line Detailtextlabel When Calculating Uitableviewcell Height (All Styles)
How to Differentiate Whether a User Is Tapping the Screen with His Finger or an Apple Pencil
Uiwebview Stringbyevaluatingjavascriptfromstring Hangs on iOS5.0/5.1 When Called Using Gcd
Custom Uitableviewcell with Progress Bar Download Update
How to Get an Error Description When Playback Fails on Mpmovieplayercontroller
Detecting iOS Version Number from User Agent Using Regular Expressions
iOS Multiple Right and Left Align on Same Line
How to Send a Uilongpressgesture Programmatically
iOS Uimenucontroller Uimenuitem, How to Determine Item Selected with Generic Selector Method
Ios: Diddiscoverperipheral Not Called in Background Mode
Xcode5:The App References Non-Public Selectors in Payload/<Appname>.App/<Appname>: Setattribution:
How to Separate Emojis Entered (Through Default Keyboard) on Textfield