admin管理员组文章数量:1336631
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?
*update* BUG is solved! this problem was fixed by facebook.
this bug was reported and also answered by facebook here-
#
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/en_US/all.js#xfbml=1";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
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?
*update* BUG is solved! this problem was fixed by facebook.
this bug was reported and also answered by facebook here-
https://developers.facebook./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/en_US/all.js#xfbml=1";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
Share
Improve this question
edited Jul 16, 2019 at 7:33
Vadim Kotov
8,2848 gold badges50 silver badges63 bronze badges
asked Jan 15, 2013 at 14:29
timzivtimziv
151 gold badge2 silver badges7 bronze badges
3 Answers
Reset to default 3I 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() {)
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
}
});
}
});
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.
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.
If i manually log-out and login again (thus using the php sdk) everything works.
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/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 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:
- 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
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.
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).
本文标签: javascriptAuto login facebookFBEventsubscribe authlogin doesn39t workStack Overflow
版权声明:本文标题:javascript - Auto login facebook - FB.Event.subscribe auth.login doesn't work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742407788a2469180.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论