且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动登录 facebook - FB.Event.subscribe auth.login 不起作用

更新时间:2023-11-17 20:54:40

我似乎遇到了同样的问题 ..

it seems i'm experiencing the same problem ..

直到几个小时前,当一个用户(之前已经授权并登录到我的应用程序)打开我的网站并且 cookie 已过期时,登录按钮出现但几秒钟后(JS SDK 的时间)触发登录功能),他自动登录,页面重新加载,没有登录按钮和用户信息.

Till a few hours ago, when a user (who had already authorized and logged in to my app previously) opened my website and the cookie was expired, the login button appeared but after a few seconds (the time for the JS SDK to trigger the login function), he was automatically logged-in and the page was reloaded without the login button and with the user information.

现在,当已经注册的用户在 cookie 过期后打开我的网站时,登录按钮再次出现,但用户不会自动登录,页面也不会像以前那样重新加载.奇怪的是,如果我点击登录按钮,它也不起作用!如果我现在手动刷新页面,登录按钮会消失并显示用户信息,因此似乎用户实际上已登录但未捕获到触发页面重新加载的事件.

Now when an already registered user opens my site after cookie expired, the login button appears again, but the user isn't automatically logged in and the page doesn't reload like it did before. The strange thing is that if i click on the login button it doesn't work either! If i now refresh the page manually the login button disappears and user information is displayed, so it seems that the user was actually logged-in but the event was not caught to trigger the page reload.

如果我手动注销并再次登录(因此使用 php sdk),一切正常.

If i manually log-out and login again (thus using the php sdk) everything works.

我的代码:

<script>
        window.fbAsyncInit = function() {
            FB.init({
                appId: '<?php echo $facebook->getAppID() ?>',
                cookie: true,
                xfbml: true,
                oauth: true
            });
            FB.Event.subscribe('auth.login', function(response) {
                window.location.reload();
            });
            FB.Event.subscribe('auth.logout', function(response) {
                window.location.reload();
                });
        };
        (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);
        }());
        function facebookLogin() {
            FB.login(function(response) {
                // handle the response
            }, {scope: 'email, publish_stream, read_friendlists'});
        }
    </script>

UPDATE 我在我的 js 代码中添加了这几行:

UPDATE I added this few lines to my js code:

FB.Event.subscribe('auth.statusChange', function(response) {
                alert('The status of the session is: ' + response.status);
            });

现在当用户在令牌过期后打开我的页面时:

So now when the users opens my page after token expired:

  • php sdk 检测到他未登录并显示登录对话框(像以前一样)
  • javascript sdk 触发登录功能(如之前)
  • 用户定期登录(像以前一样)(我知道现在可以肯定,因为它会弹出警报:会话是:已连接)
  • 但是.. 事件 auth.login 没有被触发 ->脚本不知道成功登录 -> 页面不是重新加载