views:

41

answers:

1

I have a strange problem, I have a logout link that displays only if the user is logged in. I have this code inside the the onclick event of this link (using a function):

    FB.logout();
    window.location = 'http://www.google.com';

If I click on this once, nothing happens, I know that the function gets executed because I've tested this with an alert. However, if I click on it a second time, the page reloads itself and the user is indeed logged out. The page is never directed to google.com, so the function never gets as far as the window.location part.

I have the window.location because I thought that if I could refresh the page using window.location.refresh once the logout is complete that it would successfully log the user out. However, if anybody has any other techniques on how to logout the user out of facebook, I would love to hear them! I've attempted to manually delete the cookie, but that didn't work, the cookie still existed for some reason. I've also tried this:

    FB.logout(function(response) {
    window.location = 'http://www.google.com';
    });

I know a callback like this is possible because of the documentation:

http://developers.facebook.com/docs/reference/javascript/FB.logout

Has anybody else had this issue before? Any advice would help thanks!

UPDATE: After some debugging I've found out that if I click the link once. Then manually refresh the page it logs the user out.

+1  A: 

From your description it looks to me that logout is working properly, but page refresh doesn't.

Try something like this maybe:

FB.logout(function(response) {
    window.location.reload(true);
});
serg
Thank you for your response, serg, I have tried this but it still doesn't reload once the logout has been executed. It seems that it logs me out in the background the first time, and then on the second click it realizes its not logged in to log out, if that makes sense. So it reloads the page, once it reloads my PHP realizes its logged out and displays the login form.
Pete Herbert Penito
@Pete But does it actually reload the page? Is the problem in not refreshing the page or in not logging out a user after the page is reloaded for the first time? Maybe it logs you in again after the reload on the server side?
serg
I am pretty sure that the page does reload when the link is clicked, now that you mention it. Because for one it doesn't redirect to google after the logout, and also I change the innerhtml to Logging Out... in the onclick event. And it switches right back to Log Out after a few seconds. So maybe I need a second reload?
Pete Herbert Penito
@Pete So user is logged out, page is reloaded but something logs you automatically in again. Do you handle your login on javascript side only or on php side as well? Maybe you need to notify server side that user is logged out through ajax, and after that relogin the page?
serg
I think you are onto something here, I do handle the login with PHP and Javascript. PHP doesn't seem to recognize that the user has logged out until an additional refresh is performed. I will try to alert PHP what has happened :) Thanks alot!
Pete Herbert Penito
@Pete PHP might store logged in user in a cookie which is not getting cleared. I am not sure how to log user out on php side though (not using php fb sdk)
serg