Facebook API: checking user login

I am trying to get my site to check if the user is registered on the page load, but I could not get it to work despite the following API instructions, here is 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 warning never fires until the user logs in, if he is logged off, this warning or everything that was inside this code block never starts.

+3
source share
1 answer

In the above example, remove the trailing comma (oauth: true,) //, which can only be from copy / paste, but you want to remove any gotchas

, , , .

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 only for an initial check

getLoginStatus .

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

, response.status, : , response.authResponse , : accessToken, expiresIn ..

0

All Articles