0

I am trying a simple code to verify whether the user is logged to facebook when visiting my website.

alert 1 and 2 are displayed and thats all.

Why isnt alerts 3 4 5 not shown?? Ive tried so many examples but nothing works :( :(

<!DOCTYPE html>
<html>
<head>
  <script src="http://connect.facebook.net/en_US/all.js"></script>
  <script>
    window.onload = function(){
      alert("1");

      FB.init({ apiKey: '203642573046113', status: true });

      alert("2");

      FB.getLoginStatus(function(response) {
        alert("3");
        if (response.status === 'connected') {
          alert("4");
        } else if (response.status === 'not_authorized') {
          alert("5");
        } else {
          alert("6");
        }
      });
    };
  </script>

</head>
<body>
    HELLO WORLD
</body>
</html>
Yura
  • 2,381
  • 8
  • 33
  • 44
  • I also saw that something got to do with apiKey and sandbox...http://stackoverflow.com/questions/4846951/fb-getloginstatus-doesnt-work actually, I have not app. Just want to find out whether the user is logged to facebook. So I changed the above line to: FB.init({ cookie:true, status: true }); and it still does not work :( – Yura Nov 07 '12 at 15:28
  • There's some complications with this issue: https://stackoverflow.com/questions/22889646/fb-getloginstatus-returns-status-unknown – Simon_Weaver Feb 07 '19 at 23:06

2 Answers2

1

actually, I have not app.

FB.getLoginStatus gets the user’s status regarding your app – without an app, it does not make sense to try and call this method.

Just want to find out whether the user is logged to facebook.

You can’t do that.

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • https://github.com/webarto/facebooker you could, but I reported that, I'll see if it possible now. – Dejan Marjanović Nov 07 '12 at 15:40
  • Wow! Thanks! that clears some things... Is there and "dummy app" I can use? I mean, it doesnt really ,atter for me whether the user is logged to an app and logged to facebook but not to an app... This way I would be able to know if he is logged to facebook or not. Am I right? – Yura Nov 07 '12 at 15:42
  • No. FB.getLoginStatus will only report if the user is _connected_ to the specific app. So as long as the user would not be using the app whose id you want to misuse, you would not get any useful/truthful info. – CBroe Nov 07 '12 at 15:49
  • Look: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/ means, that if I get 'non_authorized' response, that also means that the user is loggef to facebook, isnt he? – Yura Nov 07 '12 at 15:54
  • Hm, yeah, possible. But still, you’ll need your own app id, because apps are bound to domains the can “run” on – so you can’t just misuse any app’s id on your own site. – CBroe Nov 07 '12 at 15:57
  • I see thanks. Is it difficult to create some empty app? Is it free? – Yura Nov 07 '12 at 15:59
  • I don't think this was true even 7 years ago. Then what's (not_authorized) for: `The user is logged into Facebook but has not authorized your application.` ? – Simon_Weaver Feb 07 '19 at 23:04
1

The problem is this https://developers.facebook.com/bugs/1657864107810379/

i tried posting the following on that page but it gave errors.

Regarding the above issue - this applies to your question because you I assume had issued FB.Logout() at some point. This sets a cookie, which by design prevents this 'getLoginStatus' from working again.


Still an issue.

Is it working as designed - I believe it is. HOWEVER just because sometime works as designed doesn't make it not a terrible idea.

Several Stackoverflow questions reference this bug and this issue FB.getLoginStatus not working

Fundamentally this is the problem: (https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus)

The user is either not logged into Facebook or explicitly logged out of your application so it doesn't attempt to connect to Facebook and thus, we don't know if they've authenticated your application or not. (unknown)

So if you log out it sets the cookie fblo_629812369426907 (that's my app id)

Then AND THIS IS BY DESIGN IT DOESN'T CHECK LOGIN STATUS EVER AGAIN.

There needs to be FB.Logout(true) to not set a stupid FBLO cookie.

I want my users on my website to log out but still know they're logged into Facebook to make the 'Login with Facebook' option more prominent.

So it seems any Stackoverflow answers suggesting to delete this cookie are in fact correct to avoid this 'by design' behavior.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689