Check status Facebook

2

I am working with the Facebook API, more specifically with the web SDK.

I have created a login button in the index.html that when you click on it, redirects you to Facebook and in case you log in, you are redirected to the main page.

Here my question, how can I check the status of the user's connection from that home page, so that if the user is not connected the page will redirect him to the index again?

I've been searching the Facebook documentation and none of the solutions seems to work for me.

UPDATE: Doubt solved, the code that I have used finally has been the following:

    window.fbAsyncInit = function() {
        FB.init({
            appId : *AppID*,
            xfbml : true,
            cookie : true,
            version : 'v2.8'
        });
         FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            var uid = response.authResponse.userID;

        } else if (response.status === 'not_authorized') {
            window.location.replace("http://localhost:8090/index.html");
        } else {
            window.location.replace("http://localhost:8090/index.html");

        }
    });
    };

    (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id))
            return;
        js = d.createElement(s);
        js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    
asked by Josh Beausoleil 17.05.2017 в 19:06
source

1 answer

2

GetLoginStatus

Calling the function getLoginStatus you can get what you need

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    var uid = response.authResponse.userID;

  } else if (response.status === 'not_authorized') {

  } else {

  }
 });

Where you can receive:

  • connected: The user is registered on facebook and I authorize the authentication with your application.
  • not_authorized: The user is logged into Facebook but not connected to your application
  • unknown: The user is not logged in to Facebook or, I do not authorize your application

In the link of the documentation they also explain that as many times it is necessary to be constantly checking this data, you can set a boolean value of status in true when you call the function FB.init

  

To receive the answer of this call, you must subscribe to the auth.statusChange event. The response object of this event is identical to the one returned by explicitly calling FB.getLoginStatus .

    
answered by 17.05.2017 / 19:19
source