3

I'm having serious issues concerning Facebook Connect plugin. I'm trying to use this feature in order to let my users login on a website.

So I used basicaly the example given with php SDK mixing JS SDK.

I successfully managed to allow my users to login, but I can't make them logout. I'm becoming mad, I don't understand... It just doesn't work...

Later on I'll use the facebook session in a session php variable so I need to logout destroying both session and cookie. Could you just help me

Here is my code :

     <?
//uses the PHP SDK.  Download from https://github.com/facebook/php-sdk
require 'src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => 'ddddd',
  'secret' => 'ddddddddddddddd',
));

$userId = $facebook->getUser();

?>
 <?php if ($userId) { 
      $userInfo = $facebook->api('/' + $userId); 
      $params = array('next' => 'http://www.nofrenchtouch.com/include/logout.php');
      $logout = $facebook->getlogoutURL($params);?>

      <span class="facebook-in">Bienvenue <?= $userInfo['name']?> - <a href="#" onClick="logoutUser(); return false;" class="facebook-bt"> Se déconnecter</a>
</span>
    <?php } else { ?>
    <div id="fb-root"></div>
    <a href="#" class="facebook-bt" onClick="loginUser(); return false;">Se connecter avec Facebook</a>
    <?php } ?>


<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(d){
         var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/fr_FR/all.js";
         d.getElementsByTagName('head')[0].appendChild(js);
       }(document));

       function loginUser() {    
    FB.login(
        function(response) {
            getStatus(); //This is a call to a different function...
        }, 
    {scope:'email,publish_stream, user_birthday, friends_birthday'}
    );   

     function logoutUser() {    
    FB.logout(
        function(response) {
        }
    );   
}
</script>

Thanks a lot for reading.

Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48
Hugo Trial
  • 366
  • 5
  • 12

3 Answers3

0

You should use logout like this;

FB.logout(function(response) {
  FB.Auth.setAuthResponse(null, 'unknown');
  ...
});

as this refers.. Facebook JS SDK FB.logout() doesn't terminate user session

0

FB.logout(); will log the user out of Facebook. Logging out of Facebook does, essentially, end the session with your server, but I think that that is not what you are trying to achieve. Nonetheless, I have found that the following code works for logging out of Facebook:

<a href="javascript:FB.logout();void(0)">Log Out</a>

When a user grants your application privileges to his or her account, the JS SDK will automatically pass session information about the user to the server every time that a page is loaded. In order to let your user log out, you have to create your own authentication system that can be logged in and out of at will. It will use its own session hashes and whatnot that are separate from the ones that the JS SDK creates. Only use the Facebook PHP SDK to get information about your user and store it in your database. This will allow the user to log out of your website (even though the Facebook JS SDK actually still is connected). I can provide more information on user account systems if you need.

wecsam
  • 2,651
  • 4
  • 25
  • 46
0

When you are logging out of facebook remove your session and cookies of user.

function logoutUser() {    
    FB.logout(
        function(response) {
//Pass control to function in php where you can destroy your session and cookies.
// windows.location=PHP function
        }
    );   
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226