Here I am unable to keep a user logged out once the user presses the logout button from my site. He is redirected to the login page but updateButton function is called again with same credentials he is getting logged in again. I tried several ways but the problem persists. I guess I am not doing things rightly in updateButton function down here and also FB.logout() is not being done correctly, as I get the error "FB.logout called without an access token" in the console. The code is as follows:-
$(function(){
var button;
window.fbAsyncInit = function(){
FB.init({ appId : 'myAppId',
status : true,
cookie : true,
xfbml : true,
oauth : true
});
function updateButton(response) {// I am not sure I am doing it right here
console.log("Update Button Fired.");
console.log(response);
button = document.getElementById('fb-auth');
if(response.status === 'connected')
{
FB.api('/me', function(info)
{
login(response,info);
});
}
else
{
FB.login(function(response)
{
if(response.status === 'not_authorized')
{
FB.api('/me', function(info){
login(response, info);
});
}
else
{
}
}, {scope:'email, user_birthday,user_about_me' });
}
}
}
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script');
e.async = true;
e.type = 'text/javascript';
e.src = document.location.protocol + '//connect.facebook/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
})();
function login(response,info){
console.log('login Showloader called');
if (response.authResponse) {
var accessToken = response.authResponse.accessToken;
$.post("/web/register/faceBookRegistration",{ data:info,accessTokenValue:accessToken}).done( function(data){
if(typeof(data) != undefined){
window.location = "/web/login/loadViewFaceLogin";
}
});
}
}
function logout(response){
FB.logout(function(response){
console.log("User is now logged out");
});
}
});
Also I think my logout i.e
function logout(response){
FB.logout(function(response){
console.log("User is now logged out");
});
}
is not correct. In the console it shows that FB.logout called without an access token. What could be the reason
Here I am unable to keep a user logged out once the user presses the logout button from my site. He is redirected to the login page but updateButton function is called again with same credentials he is getting logged in again. I tried several ways but the problem persists. I guess I am not doing things rightly in updateButton function down here and also FB.logout() is not being done correctly, as I get the error "FB.logout called without an access token" in the console. The code is as follows:-
$(function(){
var button;
window.fbAsyncInit = function(){
FB.init({ appId : 'myAppId',
status : true,
cookie : true,
xfbml : true,
oauth : true
});
function updateButton(response) {// I am not sure I am doing it right here
console.log("Update Button Fired.");
console.log(response);
button = document.getElementById('fb-auth');
if(response.status === 'connected')
{
FB.api('/me', function(info)
{
login(response,info);
});
}
else
{
FB.login(function(response)
{
if(response.status === 'not_authorized')
{
FB.api('/me', function(info){
login(response, info);
});
}
else
{
}
}, {scope:'email, user_birthday,user_about_me' });
}
}
}
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script');
e.async = true;
e.type = 'text/javascript';
e.src = document.location.protocol + '//connect.facebook/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
})();
function login(response,info){
console.log('login Showloader called');
if (response.authResponse) {
var accessToken = response.authResponse.accessToken;
$.post("/web/register/faceBookRegistration",{ data:info,accessTokenValue:accessToken}).done( function(data){
if(typeof(data) != undefined){
window.location = "/web/login/loadViewFaceLogin";
}
});
}
}
function logout(response){
FB.logout(function(response){
console.log("User is now logged out");
});
}
});
Also I think my logout i.e
function logout(response){
FB.logout(function(response){
console.log("User is now logged out");
});
}
is not correct. In the console it shows that FB.logout called without an access token. What could be the reason
Share Improve this question edited Mar 29, 2013 at 6:12 John Doe asked Mar 28, 2013 at 13:15 John DoeJohn Doe 2,9225 gold badges49 silver badges64 bronze badges4 Answers
Reset to default 4I took the liberty to rewrite your code, keeping your bases:
// File - fb-init.js
window.fbAsyncInit = function() {
FB.init({
appId : app_id,
status : false,
cookie : true,
xfbml : true
});
};
(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";
ref.parentNode.insertBefore(js, ref);
}(document));
-
// File - main.js
$('#fb-login').on('click', function() {
FB.getLoginStatus(function(responseStatus) {
if (responseStatus.status === 'connected') {
FB.api('/me', function(responseMe) {
if (!responseMe.id)
return false;
var accessToken = responseMe.authResponse.accessToken;
$.post('/web/register/faceBookRegistration' {
data : responseMe,
accessTokenValue : accessToken
}).done(function(data) {
if (typeof(data) !== 'undefined')
window.location = '/web/login/loadViewFaceLogin';
});
});
}
else if (responseStatus.status === 'not_authorized') {
FB.login(function(responseLogin) {
FB.api('/me', function(responseMe) {
if (!responseMe.id)
return false;
var accessToken = responseMe.authResponse.accessToken;
$.post('/web/register/faceBookRegistration' {
data : responseMe,
accessTokenValue : accessToken
}).done(function(data) {
if (typeof(data) !== 'undefined')
window.location = '/web/login/loadViewFaceLogin';
});
});
}, {scope:'email, user_birthday,user_about_me' });
}
else
return false;
});
});
$('#fb-logout').on('click', function() {
FB.logout(function(responseLogout) {
});
});
Because of the status set to true. If the current user is already logged on Facebook, it forces his auto-login on the website.
Anyway, it's not a good idea to use the FB.logout(), except if you want to force the user to logout from Facebook (And not your website).
About the popup, I think it's because of FB.getLoginStatus(updateButton); which calls the function updateButton() on every page loads.
You should improve it through an onclick function.
As I saw this Post FB.logout() called without an access token it confirmed what I always was on my mind that my Logout is not being done properly. If that get done I would be okay.
Here is the logout function code which did it for me :-
function logout(response){
if(!response.authResponse){
window.location = "/web/login/";
return;
}
FB.logout(function(response){
FB.Auth.setAuthResponse(null,'unknown');
logout(response);
});
}
As in the above link its been said that we should be keep sending logout request till its destroyed at the Facebook side. I made it a recursive call and the way out was when the the check confirmed it.
I am a noob and would only be glad if somebody working on the present solution builds a more elegant hack.
Thanks to 'ZeNy' too.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744634406a4585056.html
评论列表(0条)