如果用户未登录到facebook,则在FB.init期间不会触发auth.statusChange

Ed *_*ffe 16 facebook facebook-authentication

我想知道是否有人找到了我正在经历的行为的解决方法.

以下面的代码为例:

<script>
window.fbAsyncInit = function() {
        FB.init({
          appId      : '[----removed-------]', // App ID
          channelUrl : 'http://localhost/channel.html', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies
          xfbml      : true  // parse XFBML
        });     
        // Additional initialization code here

        FB.Event.subscribe('auth.statusChange',fbLoginStatus);

        console.log("getloginstatus");

        FB.getLoginStatus(fbLoginStatus);
</script>
Run Code Online (Sandbox Code Playgroud)

使用状态设置为true的FB.init,实质上是getLoginStatus()根据facebook文档调用.但是,它显然是预期的行为,这将不会触发事件auth.statusChange,因为默认值是"未知",并且 "在没有登录"的值也是"未知"(即使它可以知道!!)

因此,如果我还想回复未登录Facebook的用户,我必须FB.getLoginStatus()明确地调用以及将状态设置为true.

问题是,如果用户不是"已注销",则会导致函数被调用两次.

有没有一种巧妙的方法来阻止这种情况发生?我认为我唯一的选择可能是调用一个不同的函数来处理身份验证更改事件.

Ed *_*ffe 14

我最好的解决方案是在fb.init选项中将状态设置为'false',然后分别显式调用getloginstatus.

如果以不知名的方式返回loginstatus(即退出),我订阅了状态更改事件,并且通常显示登录按钮.然后,当用户登录时,状态更改将按预期触发.