简体   繁体   English

直到单击按钮,才能开始Facebook登录

[英]Make facebook login not start until button clicked

Right now my facebook login code automatically logs someone in when they visit the page. 现在,我的Facebook登录代码会在某人访问该页面时自动将其登录。 Which is not good. 哪个不好

How can I make it so it only logs them in if they click the on the login button? 我该如何做,使其仅在他们单击登录按钮时才将其登录? I tried a couple of things but they didn't seem to work 我尝试了几件事,但它们似乎没有用

<script>
  // This is called with the results from from FB.getLoginStatus().
  function statusChangeCallback(response) {
    if (response.status === 'connected') {
      var accessToken = response.authResponse.accessToken;
      $.post("includes/login.php", {
            token: accessToken
        })
  .success(function() {
            location.reload();
        });
    }
  }
  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }

  window.fbAsyncInit = function() {
  FB.init({
    appId      : 'xxx',
    cookie     : true,  // enable cookies to allow the server to access 
                        // the session
    xfbml      : true,  // parse social plugins on this page
    version    : 'v2.5' // use version 2.2
  });

  FB.getLoginStatus(function(response) {
    statusChangeCallback(response);
  });

  };

  // Load the SDK asynchronously
  (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'));

</script>

<fb:login-button scope="public_profile,email,user_friends,user_birthday,user_location" onlogin="checkLoginState();">
</fb:login-button>

I've found the solution: 我找到了解决方案:

Make sure you have status: true in your init otherwise it will be blocked as a popup. 确保您的状态为:init为true,否则它将作为弹出窗口被阻止。 Hope fully this helps someone else. 希望这对别人有帮助。

<script>
  window.fbAsyncInit = function() {
  FB.init({
    appId      : 'xxx',
    cookie     : true,  // enable cookies to allow the server to access 
                        // the session
    xfbml      : true,  // parse social plugins on this page
    version    : 'v2.5', // use version 2.2
    status: true
  });

};

// Load the SDK asynchronously
  (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'));

$('#facebook').on('click', function() {
 $("#facebook").html('Logging In...');
   FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
         var accessToken = response.authResponse.accessToken;
         $.post("includes/login.php", {
            token: accessToken
         })
         .success(function() {
            location.reload();
         });
      } else if (response.status === 'not_authorized') {
        login();
      } else {
        login();
      }
   });
});

function login()
{
    FB.login(function(response) {
       if (response.authResponse) {
        var accessToken = response.authResponse.accessToken;
         $.post("includes/login.php", {
            token: accessToken
         })
         .success(function() {
            location.reload();
         });
        } else {
          $("#facebook").html('Sign In With Facebook');
        }
    }, {scope: 'public_profile,email,user_friends,user_birthday,user_location'});
}
</script>

Try this : 尝试这个 :

<script>
      // This is called with the results from from FB.getLoginStatus().
      function statusChangeCallback(response) {
        if (response.status === 'connected') {
          var accessToken = response.authResponse.accessToken;
          $.post("includes/login.php", {
                token: accessToken
            })
      .success(function() {
                location.reload();
            });
        }
      }
      function checkLoginState() {
        FB.getLoginStatus(function(response) {
          statusChangeCallback(response);
        });
      }

      window.fbAsyncInit = function() {
      FB.init({
        appId      : 'xxx',
        cookie     : true,  // enable cookies to allow the server to access 
                            // the session
        xfbml      : true,  // parse social plugins on this page
        version    : 'v2.5' // use version 2.2
      });

      };

      // Load the SDK asynchronously
      (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'));

    </script>

    <fb:login-button scope="public_profile,email,user_friends,user_birthday,user_location" onlogin="checkLoginState();">
    </fb:login-button>

     - List item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM