javascript Facebook 注销/断开连接 FB.login() 在用户已连接时调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9093942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:34:22  来源:igfitidea点击:

Facebook logout/disconnect FB.login() called when user is already connected

phpjavascriptfacebookauthorizationlogout

提问by Sergey L

<html>
    <head>
      <title>My Facebook Login Page</title>
    </head>
    <body>
      <div id="fb-root"></div>
      <script>
        window.fbAsyncInit = function() {
          FB.init({
            appId      : 'appID',
            status     : true,
            cookie     : true,
            xfbml      : true,
            oauth      : true,
          });
        };
        (function(d){
           var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
           js = d.createElement('script'); js.id = id; js.async = true;
           js.src = "//connect.facebook.net/en_US/all.js";
           d.getElementsByTagName('head')[0].appendChild(js);
         }(document));
      </script>
      <div class="fb-login-button">Login with Facebook</div>

    </body>
</html>

Every time when i click on [Login with Facebook] button i see in JS Consolethis message "FB.login() called when user is already connected."

每次当我点击 [Login with Facebook] 按钮时,我都会在JS Console此消息中看到“用户已连接时调用 FB.login()”。

How to disconnect user?

如何断开用户连接?

回答by Abirami Rajendran

Use FB.getLoginStatus method to find whether the user in logged in or not.

使用 FB.getLoginStatus 方法查找用户是否登录。

If they already logged in, try to hide the Login button. Because the login button will be visible always and do nothing on click when the user already logged in.

如果他们已经登录,请尝试隐藏“登录”按钮。因为登录按钮将始终可见,并且在用户已登录时单击时不执行任何操作。

Refer this linkfor FB.getLoginStatus

请参阅此链接以获取 FB.getLoginStatus

回答by DMCS

Good answer from Abirami, one additional item.

Abirami 的回答很好,还有一项。

How to disconnect user?

如何断开用户连接?

Disconnecting a user is done with a call similar to FB.login() called FB.logout() See: http://developers.facebook.com/docs/reference/javascript/FB.logout/

断开用户连接是通过类似于 FB.login() 的调用完成的,称为 FB.logout() 参见:http: //developers.facebook.com/docs/reference/javascript/FB.logout/

回答by Developer

This IS the solution that no one is telling anyone because it highlights a Facebook bug so be prepared for me to get voted down...

这是没有人告诉任何人的解决方案,因为它突出了 Facebook 的错误,所以准备好让我被否决......

FB.login() will get called after any user tries to logout if the 'autologoutlink' parameter on the Javascript facebook button is set to true.

如果 Javascript facebook 按钮上的“autologoutlink”参数设置为 true,则在任何用户尝试注销后,FB.login() 将被调用。

The problem is this:

问题是这样的:

    <fb:login-button autologoutlink="true" width="200" max-rows="1"></fb:login-button>

(((autologoutlink="true")))

(((autologoutlink="true")))

When true is set the login button is always called after the user logs out (FB.login) and you have set your window.location to some other page which gets redirected back to the login page, FB.login it is treated like an object, much like an app developer is.

当设置为 true 时,总是在用户注销后调用登录按钮(FB.login),并且您已将 window.location 设置为其他一些页面,该页面被重定向回登录页面,FB.login 将其视为一个对象,就像应用程序开发人员一样。

On a console you will see that FB.login() always gets called after the user logs out because of this 'autologoutlink' parameter on the Javascript facebook login button bug.

在控制台上,您会看到 FB.login() 总是在用户注销后被调用,因为 Javascript facebook 登录按钮错误上的这个“autologoutlink”参数。

I now fear for the moron that voted me down lol...

我现在害怕那个让我失望的白痴,哈哈……

YES this is a bug with the javascipt SDK asynchronously all.js script

是的,这是 javascipt SDK 异步 all.js 脚本的一个错误

The secret would be to load the javascript Facebook button without the autologoutlink="true" parameter but use the php sdk to load the logout button afterwards.

秘诀是在没有 autologoutlink="true" 参数的情况下加载 javascript Facebook 按钮,但之后使用 php sdk 加载注销按钮。

Something like this in php to put a javascript sdk login button but then post a php logout button to get around the asynchronous bug in the Facebook code:

在 php 中放置一个 javascript sdk 登录按钮,然后发布一个 php 注销按钮以解决 Facebook 代码中的异步错误:

$user = $facebook->getUser();
$logoutUrl=null;
$login_page='replace this for logged in page redirect'; //http://domain.com/loggedin
$logout_page='replace this for logged out page redirect'; //http://domain.com/loggedout

If ($user){
  $logoutUrl = $facebook->getLogoutUrl(array(
  'next'=>$logout_page
  ));
  $fb_login='<a href="'.$logoutUrl.'">Logout</a>';
}else{
  $fb_login='<fb:login-button width="200" max-rows="1"></fb:login-button>';
}

Use the javascript SDK and

使用 javascript SDK 和

FB.Event.subscribe('auth.login', function(response) {
  window.location='$login_page';
});

and somewhere on your page

和你页面上的某个地方

echo $fb_login;

So you use the Javascript login and the php logout redirect methods and it will patch the bug on the Javascript login button autologoutlink="true" parameter which we will not use, you will just need a logout button image and css and FB.login() will not get called anymore after FB.logout() each time.

因此,您使用 Javascript 登录和 php 注销重定向方法,它将修补我们不会使用的 Javascript 登录按钮 autologoutlink="true" 参数上的错误,您只需要一个注销按钮图像和 css 以及 FB.login( ) 不会在每次 FB.logout() 之后被调用。

You can see how to shorten this a whole bunch I'm sure but this is the fix that no one wants to tell you for looking.

我敢肯定,您可以看到如何缩短这一整批,但这是没有人愿意告诉您寻找的解决方案。

And now for my Pointless Rant...

现在对于我毫无意义的咆哮......

In short, if they run a totally unsecure session in the PHP SDK and they can not even make a button work then don't look to them for answers!.

简而言之,如果他们在 PHP SDK 中运行一个完全不安全的会话并且他们甚至无法使按钮工作,那么不要向他们寻求答案!。

You are forced to use what Facebook are defining as asynchronous scripts however they are only one sided security, anyone can spoof a domain or hiHyman an unsecure session where all payments are handled by said spoofed domain ergo any app on Facebook is totally and utterly hackable and they know it, they just haven't figured out a way to shovel you their BS apart from the word asynchronous which is NOT two way security is it.

您被迫使用 Facebook 定义为异步脚本的内容,但它们只是一方面的安全性,任何人都可以欺骗域或劫持不安全的会话,其中所有付款都由所述欺骗域处理,因此 Facebook 上的任何应用程序都完全可以被黑客入侵,并且他们知道这一点,除了异步这个词之外,他们还没有想出一种方法来铲除他们的 BS,这不是两种方式的安全性。

Try design anything that is hooked using an asynchronous JAVASCRIPT and then make is secure, they can always blame YOU for the fact they can not deliver a button that says the user is or is not connected in any simple terms with today's scripting and technology.

尝试设计任何使用异步 JAVASCRIPT 挂钩的东西,然后 make 是安全的,他们总是可以责怪您,因为他们无法提供一个按钮,以任何简单的术语说明用户是否与当今的脚本和技术相关联。

I'm laughing right now.

我现在在笑。

I guess that's why we are on stackoverflow...

我想这就是我们在 stackoverflow 上的原因...

Personally I think the Facebook javascript and php SDk should be simple and not force the designer to learn their own insane methods in social design but simply provide a method that says YES the user is logged in so... or NO the user isn't so... but Facebook seem to think they are adobe and con-volute everything with kak leaving us to read a four story building of books on their personal philosophy of design to only be left with no examples and code that you would have to be psychic to debug for which will only be changed two days after you finish building your app which took fourteen years just because they fail to deliver and instead like to sound like they know what they are doing to cover so many revisions that are made to remove said bugs.

我个人认为 Facebook javascript 和 php SDk 应该很简单,不要强迫设计师学习他们自己在社交设计中的疯狂方法,而只是提供一种方法,说“是”,用户已登录,所以……或“否”,用户不是所以……但 Facebook 似乎认为他们是土坯,把一切都与 kak 纠缠不清,让我们去阅读关于他们个人设计哲学的四层楼的书,只留下你必须要做的例子和代码调试的通灵者只会在您完成构建您的应用程序两天后更改,这花了十四年的时间只是因为它们未能交付,而是听起来好像他们知道他们正在做什么以涵盖如此多的修改以删除说错误。

Like adobe, if it worked, we wouldn't have to do a course in it's design would we ?

像 adobe 一样,如果它有效,我们就不必上它的设计课程,对吗?

But as it is shite and wastes billions of hours of human resources then I'm sure at some point you will be forced to pay for a qualification in it because that means more money right ?.

但是因为它很糟糕并且浪费了数十亿小时的人力资源,所以我相信在某个时候你会被迫支付资格,因为这意味着更多的钱,对吧?。

I mean you have to pass all payments through Facebook already in such a way that they can remotely edit your sessions ergo your app ergo your database ergo your payments.

我的意思是你必须已经通过 Facebook 传递所有付款,以便他们可以远程编辑你的会话,你的应用程序,你的数据库,你的付款。

What TF has your payments got to do with Facebook when what you deliver is what they should be paying you for and ultimately brings, keeps and delivers them more users.

当您交付的内容是他们应该支付给您的并最终为他们带来、留住和交付更多用户时,TF 与您的付款与 Facebook 有什么关系。

Same with Hollywood, they call piracy a free film on the internet instead of paying you for the free advertising of their products while Hollywood take 80 billion and hour in interest, ban interest!.

与好莱坞一样,他们将盗版称为互联网上的免费电影,而不是为他们产品的免费广告付费,而好莱坞则收取 800 亿小时的利息,禁止利息!。

Piracy is making money from others work not slavery and to be sued for it or actually pay for doing it.

盗版是从他人的工作中赚钱,而不是奴隶制,并因此被起诉或实际为此付出代价。

Remember this lecture it will do you well.

记住这个讲座,它会对你有好处。

I said to much LOL...

我说了很多 LOL...

When it is all clear you will see just where the road is taking us.

当一切都清楚时,您将看到道路将带我们走向何方。