javascript 自动登录 facebook - FB.Event.subscribe auth.login 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14339899/
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
Auto login facebook - FB.Event.subscribe auth.login doesn't work
提问by timziv
for some reason today my code for auto login for existed members broke. normally this code works perfect, after a check with firebug I saw that there is a problem finding 'auth.login' for some reason, and therefor the call for window.location.reload(); isn't working. (if I refresh manually then the member shows as connect properly,the problem is just the auto login before the session is open that need a page refresh) I also tried to see if the problem is only 'auto.login' and replaced it with 'auth.authResponseChange' when I did it the page kept on refreshing ... so I figure that problem is with 'auth.login' trigger. does anyone knows for a special reason or any change facebook made that could cause this problem?
出于某种原因,今天我为现有成员自动登录的代码坏了。通常这段代码可以完美运行,在检查 firebug 后,我发现由于某种原因在查找 'auth.login' 时出现问题,因此调用 window.location.reload(); 不工作。(如果我手动刷新然后成员显示为正确连接,问题只是在会话打开之前自动登录需要刷新页面)我还尝试查看问题是否只是“auto.login”并将其替换为'auth.authResponseChange' 当我这样做时,页面不断刷新......所以我认为问题出在 'auth.login' 触发器上。有没有人知道出于特殊原因或 facebook 所做的任何可能导致此问题的更改?
*update*BUG is solved! this problem was fixed by facebook.
*更新*BUG 解决了!这个问题是由 facebook 修复的。
this bug was reported and also answered by facebook here-
此错误已报告并由 facebook 在这里回答-
https://developers.facebook.com/bugs/524245490930206?browse=search_50f87d9e8869a5e06191882#
https://developers.facebook.com/bugs/524245490930206?browse=search_50f87d9e8869a5e06191882#
BTW,Here is A WORKING AGAIN code:
顺便说一句,这是一个重新工作的代码:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php =$APP_ID ?>',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true
});
// Additional initialization code here
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
采纳答案by Marco Gagliardi
it seems i'm experiencing the same problem ..
看来我遇到了同样的问题..
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过期时,登录按钮出现但几秒钟后(JS SDK触发的时间)登录功能),他自动登录并重新加载页面,没有登录按钮和用户信息。
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.
现在,当已经注册的用户在 cookie 过期后打开我的网站时,登录按钮再次出现,但用户不会自动登录,页面也不会像以前那样重新加载。奇怪的是,如果我点击登录按钮,它也不起作用!如果我现在手动刷新页面,登录按钮会消失并显示用户信息,因此似乎用户实际上已登录但未捕获到触发页面重新加载的事件。
If i manually log-out and login again (thus using the php sdk) everything works.
如果我手动注销并再次登录(因此使用 php sdk),一切正常。
My code:
我的代码:
<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>
UPDATEI added this few lines to my js code:
更新我将这几行添加到我的 js 代码中:
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:
所以现在当用户在令牌过期后打开我的页面时:
- the php sdk detects he's not logged in and displays the login dialog (like before)
- the javascript sdk triggers the login functions (like before)
- the user gets regularly logged (in like before) (i can know it for sure now because it pops up the alert: The status of the session is:connected)
- BUT.. the event auth.login is not triggered -> the script isn't aware of succeeded log in -> the page is not reloaded
- php sdk 检测到他没有登录并显示登录对话框(像以前一样)
- javascript sdk 触发登录功能(像以前一样)
- 用户定期登录(像以前一样)(我现在可以肯定地知道它,因为它会弹出警报:会话的状态是:已连接)
- 但是 .. 事件 auth.login 未触发 -> 脚本不知道成功登录 -> 页面未重新加载
回答by Eric Schleeper
I changed from auth.login to auth.statusChange, and also wrapped everything in getLoginStatus, so it runs only when it is not connected, it seems to be working ok. Looks like this (inside window.fbAsyncInit = function() {)
我把auth.login改成了auth.statusChange,也把所有的东西都封装在getLoginStatus里,所以它只有在没有连接的时候才运行,看起来工作正常。看起来像这样(window.fbAsyncInit = function() {)
FB.getLoginStatus(function(response) {
if (response.status != 'connected') {
FB.Event.subscribe('auth.statusChange', function(response) {
if (response.authResponse) {
// whatever code runs when user logs in
}
});
}
});
回答by breez
I was having the exact same problem. The javascript SDK made the users auto-login with a page reload if they connected my app and their access token had expired.
我遇到了完全相同的问题。如果用户连接了我的应用程序并且他们的访问令牌已过期,javascript SDK 会使用户自动登录并重新加载页面。
I solved it by switching to auth.statusChange instead of auth.login. Then I check with the PHP Facebook SDK if the user is logged in and if he is, i don't subscribe to the auth.statusChange event in javascript. (If you do include the auth.statusChange in javascript when the user is logged in, you will end up in an infinite page refresh loop, at least that was the case with my implementation).
我通过切换到 auth.statusChange 而不是 auth.login 解决了这个问题。然后我检查 PHP Facebook SDK 是否用户已登录,如果他是,我不会订阅 javascript 中的 auth.statusChange 事件。(如果您在用户登录时在 javascript 中包含 auth.statusChange,您最终将进入无限页面刷新循环,至少我的实现就是这种情况)。