Is there any way to know if a user already logged to Facebook ?

At first, in my app (on iOS), if the user uses it for the first time, he can login to facebook by tapping a “Facebook Connect” button. I’m using **facebook.login(appID, facebookListener). **Then, he can post status and photos (facebook.login(fbAppID, facebookListener, {“publish_actions”}))

But once he leaves the app, if he launches it again, the “Facebook Connect” button is obviously still there and I’d like to hide it if the login has already been done or put a “Disconnect from Facebook” button.

So, is there any way to know if the user already logged to Facebook ?

Hi @evanspro,

I’m not an expert on Facebook functionality, but perhaps you could use the “fbconnect” event and its various properties to check for this. For example, when the app starts up (in the “applicationStart” or “applicationResume” system event), do a “facebook.login()” and in that listener, check for “event.type” to be “session”. If it is, then check if “event.phase” is “login”, which indicates that the user is already logged in. Based on that, you could then take some action relating to the button. Again, there might be a better way, but this would be one method to explore.

Here are some links to the docs:

http://docs.coronalabs.com/api/event/fbconnect/index.html

http://docs.coronalabs.com/api/event/fbconnect/phase.html

Best regards,

Brent

Hi @evanspro,

I’m not an expert on Facebook functionality, but perhaps you could use the “fbconnect” event and its various properties to check for this. For example, when the app starts up (in the “applicationStart” or “applicationResume” system event), do a “facebook.login()” and in that listener, check for “event.type” to be “session”. If it is, then check if “event.phase” is “login”, which indicates that the user is already logged in. Based on that, you could then take some action relating to the button. Again, there might be a better way, but this would be one method to explore.

Here are some links to the docs:

http://docs.coronalabs.com/api/event/fbconnect/index.html

http://docs.coronalabs.com/api/event/fbconnect/phase.html

Best regards,

Brent

Hi Brent !

I put this problem aside for a moment, and totally forgot to answer this post : thank you for answering !

I’ll try this as soon as I get in it again :slight_smile:

Ok, I got in it again ! And it seems to works, although it needs some tweak. I won’t be able to post all the code, since mine is kinda heavy (lot of stuff going on, not related to this problem). So this is what I had before :

A “Facebook Connect button”, calling this function :

function triggerFacebookLogin(event) facebook.login(fbAppID, facebookListener) end facebookLoginButton:addEventListener("touch", triggerFacebookLogin)

A “Share Button”, calling this function :

function triggerFacebookShare(event) fbCommand = POST\_PHOTO facebook.login(fbAppID, facebookListener) end facebookShareButton:addEventListener("touch", triggerFacebookShare)

The facebookListener is basically what we already have in the Corona’s Facebook Sample “Scrumptious”.

So far, here’s what a user had to do in order to post on Facebook, for the first time :

  • The user launches the Corona App.
  • He has to touch the “Facebook  Connect Button” : the app swapps to Facebook, then it asks him to authorize his Facebook App to retrieve his login informations.
  • Once the user accepts, it switches back to the Corona app, then he has to touch the “Share Button”. It swapps again to the Facebook App so that the user authorizes to publish things on his wall.
  • Once the user accepts (again), it switches back to the Corona App. Then, he needs to touch (again) the “Share Button” so that the picture gets posted.

If the user has already posted at least one picture (and had to go through all those steps), if he wants to post an other picture, he has to :

  • Launch the Corona App.
  • The user touches the “Facebook Connect Button” : it swapps to the Facebook App then automatically goes back to the Corona Back (since the user is already logged)
  • He touches the “Share Button” : the pictures gets posted.

So, as you can see, if the user does it for the first time, he has to go through the “double identification process”, which is really annoying, but mendatory. Later, I actually managed to simplify the process by having one single button : the function called simply changes once he logins to Facebook, which is a lot simplier for the user.

But, if the users do it a second time, he still has to go through the “Facebook Connect Login” process, which is useless since he has already done that.

And this is where I was a few months ago when I asked : " Is there any way to know if a user already logged to Facebook".

Well, the short answer is : yes, there is. And it’s quite simple actually.

I tried what Brent suggested but the problem was it automatically swapped to the Facebook App everytime the user launches the Corona App (tell me if I’m wrong, but it seems everytime it calls facebook.login, the app swapping is necessary).

So, the Facebook Listener returns an expiration event. Which is simply a timecode (just like os.time), corresponding to the lifetime duration of the Facebook login token. By the way, the duration is now set to 60 days.

Once the user successfully logins, the expiration event is saved in his savegame. Everytime he launches the app, it first verify the expiration date is still OK, or not. If it’s not, then the user has to go through the login process again.

I’ve still got to find out what happens if the users removes the Facebook App from his profile, and how to deal with it. But it seems to work ! And, i’m also wondering if the event.expiration depends on “Facebook local time” or “User device time” which could be a problem, since users do seem to change their “user device time” because of some games like Candy Crush…

If you see any other way or if I’m totally wrong, don’t hesitate to tell me !

Hi Brent !

I put this problem aside for a moment, and totally forgot to answer this post : thank you for answering !

I’ll try this as soon as I get in it again :slight_smile:

Ok, I got in it again ! And it seems to works, although it needs some tweak. I won’t be able to post all the code, since mine is kinda heavy (lot of stuff going on, not related to this problem). So this is what I had before :

A “Facebook Connect button”, calling this function :

function triggerFacebookLogin(event) facebook.login(fbAppID, facebookListener) end facebookLoginButton:addEventListener("touch", triggerFacebookLogin)

A “Share Button”, calling this function :

function triggerFacebookShare(event) fbCommand = POST\_PHOTO facebook.login(fbAppID, facebookListener) end facebookShareButton:addEventListener("touch", triggerFacebookShare)

The facebookListener is basically what we already have in the Corona’s Facebook Sample “Scrumptious”.

So far, here’s what a user had to do in order to post on Facebook, for the first time :

  • The user launches the Corona App.
  • He has to touch the “Facebook  Connect Button” : the app swapps to Facebook, then it asks him to authorize his Facebook App to retrieve his login informations.
  • Once the user accepts, it switches back to the Corona app, then he has to touch the “Share Button”. It swapps again to the Facebook App so that the user authorizes to publish things on his wall.
  • Once the user accepts (again), it switches back to the Corona App. Then, he needs to touch (again) the “Share Button” so that the picture gets posted.

If the user has already posted at least one picture (and had to go through all those steps), if he wants to post an other picture, he has to :

  • Launch the Corona App.
  • The user touches the “Facebook Connect Button” : it swapps to the Facebook App then automatically goes back to the Corona Back (since the user is already logged)
  • He touches the “Share Button” : the pictures gets posted.

So, as you can see, if the user does it for the first time, he has to go through the “double identification process”, which is really annoying, but mendatory. Later, I actually managed to simplify the process by having one single button : the function called simply changes once he logins to Facebook, which is a lot simplier for the user.

But, if the users do it a second time, he still has to go through the “Facebook Connect Login” process, which is useless since he has already done that.

And this is where I was a few months ago when I asked : " Is there any way to know if a user already logged to Facebook".

Well, the short answer is : yes, there is. And it’s quite simple actually.

I tried what Brent suggested but the problem was it automatically swapped to the Facebook App everytime the user launches the Corona App (tell me if I’m wrong, but it seems everytime it calls facebook.login, the app swapping is necessary).

So, the Facebook Listener returns an expiration event. Which is simply a timecode (just like os.time), corresponding to the lifetime duration of the Facebook login token. By the way, the duration is now set to 60 days.

Once the user successfully logins, the expiration event is saved in his savegame. Everytime he launches the app, it first verify the expiration date is still OK, or not. If it’s not, then the user has to go through the login process again.

I’ve still got to find out what happens if the users removes the Facebook App from his profile, and how to deal with it. But it seems to work ! And, i’m also wondering if the event.expiration depends on “Facebook local time” or “User device time” which could be a problem, since users do seem to change their “user device time” because of some games like Candy Crush…

If you see any other way or if I’m totally wrong, don’t hesitate to tell me !