Facebook Login Button: Can I prevent auto page reload after login?

霸气de小男生 提交于 2020-01-15 07:56:06

问题


I'm trying to upgrade to the 3.1 SDK and running into some problematic behavior: When I log in via the FB login button on my site, the page reloads, despite the fact that I have my own JS method defined in the login button's "onlogin" event.

Because the page instantly reloads, it appears that my own "onlogin" script (which checks the FB user and logs them into MY site) seems to get cut off or does not fire. Thus, the user cannot log in to the site.

The other side effect is peculiar: After logging into FB vi my site's FB button, I will show up as being logged in at Facebook.com, but after a few seconds, a popup will appear at Facebook.com saying "Not logged in. Please log in to continue (YES NO)."

As usual I can't find help in the docs and am getting really stumped. Here is my FB init:

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId: '<?php echo cConfiguration::$facebookAppId;?>',
        cookie: true,
        xfbml: true,
        oauth: true
    });
    FB.Event.subscribe('auth.logout', function(response) {
        window.location = '/signout.php5';
    });
};
(function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
        '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>   

The login button is:

<fb:login-button length="long" autologoutlink="false" onlogin="fbLogin();"></fb:login-button>

EDIT I've also tried this button code without any of the options or the "onlogin" event and I get the same behavior.

So my questions:

  • Is it expected that the page should reload after one logs in via the login button, even though have not subscribed the auth.login event in my FB.init?
  • If so, is there a way to override that behavior?
  • Is something else wrong here?

回答1:


You have attached an event handler for logout but not for login. Add this.

FB.Event.subscribe('auth.login', function(response) {
return false;
});

Although, preventing the reload is probably not what you want to do. Instead what I have found works well is to detect if the user has clicked the login button or not. THe process is.

  1. On page load (well actually after FB.init) facebook will automatically login.
  2. You should handle this login and log the user into your site via an ajax call. After this login is successful set a JS variable that states the user is logged in.
  3. Attach an event to the login button. Once the user 'clicks' the login button check to see if they are logged in and refresh the page.



回答2:


Ok, I found out the cause of seemingly uncontrollable page reloads

For some reason the auth.logout event was being triggered whenever I would log IN via the Facebook login prompt.

So I changed this line:

FB.Event.subscribe('auth.logout', function(response) {
    window.location = '/signout.php5';
});

to

FB.Event.subscribe('auth.logout', function(response) {
    return false;
});



回答3:


Use this as your Login Button, i believe there must be something in your fbLogin(); param that is reloading the page. Normal Behaviour is to just refresh the button, page refresh has to be forced with Event.subscribe.

<fb:login-button autologoutlink="false"></fb:login-button>


来源:https://stackoverflow.com/questions/7197754/facebook-login-button-can-i-prevent-auto-page-reload-after-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!