Javascript Facebook JS SDK FB.logout() 不会终止用户会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8817569/
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
Facebook JS SDK FB.logout() doesn't terminate user session
提问by Casey Flynn
I'm attempting to log a user out of facebook with the Facebook JS SDK, however calling:
我正在尝试使用 Facebook JS SDK 将用户从 facebook 注销,但是调用:
FB.logout(function(response){
console.log(response);
});
returns: response.status == "connected"
返回: response.status == “已连接”
And only after refreshing the page does the SDK realize that the session has ended. Anyone know what could be causing this behavior? This code previously worked in my application and has recently started behaving this way.
并且只有在刷新页面后,SDK 才意识到会话已经结束。任何人都知道可能导致这种行为的原因是什么?这段代码以前在我的应用程序中有效,最近开始以这种方式运行。
Another example using FireBug:
另一个使用 FireBug 的例子:
采纳答案by Sean Kinsey
https://developers.facebook.com/bugs/245362365535898?browse=search_4f112135664703a96454690This is a bug in the JS SDK that has now been fixed and it should get pushed in not too long.
Until then you can do the following
https://developers.facebook.com/bugs/245362365535898?browse=search_4f112135664703a96454690这是 JS SDK 中的一个错误,现在已经修复,应该不会被推送太久。
在此之前,您可以执行以下操作
FB.logout(function(response) {
FB.Auth.setAuthResponse(null, 'unknown');
...
});
回答by NavCore
FB.logout(function(response) {
...
});
This FB.logout method is working 100%. The problem is that users are trying to call it from localhost which is not working.
这个 FB.logout 方法工作 100%。问题是用户试图从无法正常工作的本地主机调用它。
Please try call it from server.
请尝试从服务器调用它。
回答by Roger
See http://hustoknow.blogspot.com/2012/01/dealing-with-zombie-facebook-cookies.html
见http://hustoknow.blogspot.com/2012/01/dealing-with-zombie-facebook-cookies.html
When you logout, a cross-domain request gets sent to Facebook to invalidate the session. When you hit reload, another request gets sent to Facebook's site -- since FB recognizes the cookie as invalid, it correctly deletes the cookie from your browser.
当您注销时,跨域请求会发送到 Facebook 以使会话无效。当您点击重新加载时,另一个请求被发送到 Facebook 的网站——因为 FB 将 cookie 识别为无效,它会正确地从您的浏览器中删除 cookie。
I suspect it's a regexp bug in how they forgot to parse the fbm_ cookie, recently introduced in the last day or so. I'm just surprised that this fix hasn't been pushed.
我怀疑这是他们忘记解析最近在最后一天左右引入的 fbm_ cookie 的正则表达式错误。我只是很惊讶这个修复程序没有被推送。
回答by vishnu lal
As i have spent lots of time to find out exactly what is happening in the FB logout, felt better to share it here for others.
由于我花了很多时间来确切了解 FB 注销中发生的情况,因此在这里与其他人分享感觉更好。
First thing, Please read the documentation here
首先,请阅读此处的文档
A person logs into Facebook, then logs into your app. Upon logging out from your app, the person is still logged into Facebook.
一个人登录 Facebook,然后登录您的应用。从您的应用程序注销后,此人仍会登录 Facebook。
this point killed 90% my of time. When i have logged in from facebook.com and trying to test login button in my application, it worked as expected, but logout was not terminating the session.
这一点杀死了我 90% 的时间。当我从 facebook.com 登录并尝试在我的应用程序中测试登录按钮时,它按预期工作,但注销并未终止会话。
Solution or Fix: As per the scenarios given in the document, it will not terminate the user session, as the login is not initiatedfrom the app, it is from facebook.com. so in this case, fb will not terminate the session.
解决方案或修复:根据文档中给出的场景,它不会终止用户会话,因为登录不是从应用程序发起的,而是来自 facebook.com。所以在这种情况下,fb 不会终止会话。
When you login into fb, from your app (fresh login as username and password), system consider the source/trigger of the session as your app. so when you do logout (window.FB.logout or FB.logout) it will terminate user session completely.
当您登录到 fb 时,从您的应用程序(以用户名和密码重新登录),系统将会话的来源/触发器视为您的应用程序。因此,当您注销(window.FB.logout 或 FB.logout)时,它将完全终止用户会话。
So please logout from facebook.com before testing login functionality in your app.
因此,在您的应用程序中测试登录功能之前,请先从 facebook.com 注销。
As other mentioned, use below code to logout
正如其他人提到的,使用下面的代码注销
FB.logout(function(response) {
// response from logout will have authResponse with access_token so better to test the status as it will return "unknown"
if(response.status !== "connected") { } \ do some check on the status of the login before considering successful logout.
});
Last but not least: to test from localhost, please update the settings in FB APP
最后但并非最不重要的是:要从本地主机进行测试,请更新 FB APP 中的设置
- Update App Domainsto localhost
- Site URLunder Websitesto http://localhost:3000/
- 将应用程序域更新为本地主机
- 网站下的网站URL为http://localhost:3000/
This will solve issues faced in testing from localhost
这将解决从本地主机进行测试时面临的问题
回答by Abdul Ahad
I've faced similar kind of problem. I've used FB.getLoginStatus()
after Logout.
我遇到过类似的问题。我是FB.getLoginStatus()
注销后用的。
回答by Pat James
I have the same experience with the FB.Logout()
not working as advertised. As a workaround I use the below javascript function to check if the user is logged in and if so, redirect to https://www.facebook.com/logout.phpwith the URL of the subsequent page to load and their access token:
我有同样的经历,但FB.Logout()
没有像宣传的那样工作。作为一种解决方法,我使用以下 javascript 函数来检查用户是否已登录,如果已登录,则使用要加载的后续页面的 URL 及其访问令牌重定向到https://www.facebook.com/logout.php:
function reallylogout() {
FB.getLoginStatus(function (response) {
if (response.authResponse) {
window.location = "https://www.facebook.com/logout.php?next=" +
'URL to redirect to' +
"&access_token=" + response.authResponse.accessToken;
} else {
$("#loginButtonDiv").show();
$("#logoutButtonDiv").hide();
}
});
}
The show/hide bit is just jQuery to show or hide divs that have a login and logout button in them. The logout button's onclick triggers the reallyLogout() function.
显示/隐藏位只是用于显示或隐藏其中具有登录和注销按钮的 div 的 jQuery。注销按钮的 onclick 触发了 realLogout() 函数。
This works for my app.
这适用于我的应用程序。
回答by sajanyamaha
Ok guys I found a solution for this:
好的,我找到了解决方案:
Put up onClick event for the logout <a>
tag like this
为这样的注销<a>
标签设置 onClick 事件
onclick="FB.logout(function() { document.location.reload(); });"
All together:
全部一起:
<a href="#"
id="auth-logoutlink"
style="float:left;font-size: small;"
onclick="FB.logout(function() { document.location.reload(); });">
[logout]
</a>
回答by aamir sajjad
in asp.net mvc application i did it as following.
在 asp.net mvc 应用程序中,我按照以下方式进行了操作。
<a href="javascript:void(0);" class="logOff">Log out</a>
$(function () {
$(".logOff").click(function () {
//check if logout is
FB.getLoginStatus(function (response) {
console.log('inside login status');
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
FB.logout(function (response) {
FB.Auth.setAuthResponse(null, 'unknown');
});
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
console.log('response status not logged in');
}
});
window.location.href = '@Url.Action("LogOff", "Account")';
});
回答by Shaun
It appears broken in Facebook. On FB rell you can login but the logout function does nothing. http://www.fbrell.com/auth/all-in-one
它在 Facebook 中似乎已损坏。在 FB rell 上,您可以登录,但注销功能不执行任何操作。 http://www.fbrell.com/auth/all-in-one
回答by Amine
It may be doesn't work because you don't include your Facebook app key. It works for me like this:
它可能不起作用,因为您没有包含您的 Facebook 应用程序密钥。它对我来说是这样的:
Ext.get('auth-logoutlink').on('click', function(){
FB.getLoginStatus(function (response) {
if (response.authResponse) {
window.location = "https://www.facebook.com/logout.php?confirm=1&api_key=**MYAPIKEY**&next=" +
'**MYWEBSITEURL**' +
"&access_token=" + response.authResponse.accessToken;
} else {
//HIDE **LOGOUT BUTTON**
//SHOW **LOGIN BUTTON**
}
});
});