Handing Firebase + Facebook Login Process

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 :

  1. If a brand new user taps the FB login button on the login screen, they'll be taken to the setup screen.

Fulfilled.


  1. 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


  1. 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



Leave a reply



Submit