最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect if facebook user is logged in with no popups - Stack Overflow

programmeradmin1浏览0评论

I am trying to check if the user is logged in in facebook (and redirect to other page) but I can't open any popup for this:

This will work: but would open a popup in case the user is NOT logged in facebook

FB.login(function(response) {
    if(response.status == 'connected')
              /*Redirect here*/
});

This is working in another web of mine, but not here

window.fbAsyncInit = function() {
                FB.init({
                  appId      : myAppid, // App ID 
                  status     : true, // check login status
                  cookie     : true, // enable cookies to allow the server to access the session
                  xfbml      : true  // parse XFBML
                });
                FB.Event.subscribe('auth.login', function() {
                      /* REDIRECT HERE */
                });
              };
              // Load the SDK Asynchronously
              (function(d){
                 var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
                 if (d.getElementById(id)) {return;}
                 js = d.createElement('script'); js.id = id; js.async = true;
                 js.src = "//connect.facebook/es_ES/all.js";
                 ref.parentNode.insertBefore(js, ref);
               }(document));

Any clue here?

I am trying to check if the user is logged in in facebook (and redirect to other page) but I can't open any popup for this:

This will work: but would open a popup in case the user is NOT logged in facebook

FB.login(function(response) {
    if(response.status == 'connected')
              /*Redirect here*/
});

This is working in another web of mine, but not here

window.fbAsyncInit = function() {
                FB.init({
                  appId      : myAppid, // App ID 
                  status     : true, // check login status
                  cookie     : true, // enable cookies to allow the server to access the session
                  xfbml      : true  // parse XFBML
                });
                FB.Event.subscribe('auth.login', function() {
                      /* REDIRECT HERE */
                });
              };
              // Load the SDK Asynchronously
              (function(d){
                 var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
                 if (d.getElementById(id)) {return;}
                 js = d.createElement('script'); js.id = id; js.async = true;
                 js.src = "//connect.facebook/es_ES/all.js";
                 ref.parentNode.insertBefore(js, ref);
               }(document));

Any clue here?

Share edited Aug 17, 2017 at 10:12 Talha Awan 4,6194 gold badges26 silver badges40 bronze badges asked May 22, 2012 at 8:34 Toni Michel CaubetToni Michel Caubet 20.2k58 gold badges217 silver badges388 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You can use FB.getLoginStatus to get the info you want.

The first time in the current browser session that FB.getLoginStatus is called, or the JS SDK is init'd with status: true, the response object will be cached by the SDK. Subsequent calls to FB.getLoginStatus will return data from this cached response.

Example from documentation:

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // the user is logged in and has authenticated your
    // app, and response.authResponse supplies
    // the user's ID, a valid access token, a signed
    // request, and the time the access token 
    // and signed request each expire
    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;
  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    // but has not authenticated your app
  } else {
    // the user isn't logged in to Facebook.
  }
});
发布评论

评论列表(0)

  1. 暂无评论