Facebook API: checking if user is logged in on connect

I have been trying to get my website to check if the user is logged in on page load but I have not been able to get it to work despite following API instructions, here's an example:

<!DOCTYPE html>
    <html>
        <head>
             <title>My Facebook Login Page</title>
        </head>
        <body>
            <div id="fb-root"></div>
            <script>
                alert('hello');
                window.fbAsyncInit = function() {
                    FB.init({
                        appId  : 'APP_ID',
                        status : true, 
                        cookie : true,
                        xfbml  : true,
                        oauth  : true
                    });

                    FB.getLoginStatus(function( response ) {
                       console.log(response);
                       alert('hello2');  
                    });

                    FB.Event.subscribe('auth.statusChange', function(response) {
                       alert('hello2');
                    });

                    FB.Event.unsubscribe('auth.statusChange');
               };
               (function(d){
                   var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
                   js = d.createElement('script'); js.id = id; js.async = true;
                   js.src = "//connect.facebook.net/en_US/all.js";
                   d.getElementsByTagName('head')[0].appendChild(js);
               }(document));
           </script>
       </body>
    </html>

The alert is never even hit unless the user is logged in, if he is logged out, this alert or anything that would be inside that code block never triggers.

Answers


In your example above remove the trailing comma (oauth: true,) // that might just be from copy/paste but want to eliminate any gotchas

For debugging, console logging is my preferred method but since you're not even seeing alerts, your situation is a little bit tricky.

FB.Event.subscribe("auth.statusChange", function(response) {
  console.log(response);
});

FB.Event.unsubscribe("auth.statusChange");
// If you were to get subscribe to work, think about unsubscribing at some point to, since most likely that's only for an initial check

Alternatively, try using the getLoginStatus instead of the event subscription.

FB.getLoginStatus(function( response ) {
    console.log(response);  
});

Response should give you an object and you can check response.status and its possible values are: connected, not_authorized or unknown response.authResponse will include properties like: accessToken, expiresIn, etc.


Need Your Help

JS/JQuery: Dynamically add “pattern” and “title” attribute to form input

javascript jquery html html5 forms

I have multiple form input elements. I would like to add a pattern attribute and a title attribute to each one, but rather than do so manually, I want to dynamically add the same pattern and title...

UITableView Core Data reordering

iphone objective-c cocoa-touch uitableview core-data

I know this question has been asked before, and I took a look at the answer to this question. However, I'm still confused as to how to implement reordering with a UITableView in a Core Data project...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.