webtechnick / CakePHP-Facebook-Plugin

CakePHP Facebook Plugin
http://facebook.webtechnick.com
445 stars 138 forks source link

Connect: Session logout without Facebook logout #97

Open dgrabla opened 11 years ago

dgrabla commented 11 years ago

Hi,

I want the users to logout from the app, without needing to logout from Facebook first. In another way, in doesn't matter if the user is logged on Facebook, the app will not do the Auth->login() unless the user clicks on the login to Facebook button.

First let's say that if the Facebook plugin is deactivated, the application works without issues. Users can be created, login and logout. Only when I activate the plugin the problems arise. Can this plugin coexist with CakePHP Auth for users that want to use the site without Facebook Connect?

The normal Facebook->logout() function calls the logout() javascript function from Facebook, which destroy the Facebook session altogether. The user is logout from the App (Because the users->logout() is also called) and from Facebook, but the moment the user logins in Facebook again, he is logged in the App as well.

I tried an alternative method, doing a Session->destroy() and then deleting (putting the expiration date of the cookies in the past) all the cookies (My app session cookie + several facebook cookies). This doesn't work very well because if I refresh the page a couple of times, eventually the Facebook javascript code regenerates his cookie and then the user is automatically logged in the APP.

I tried using noAuth: 'Facebook.Connect' => array('model' => 'User','createUser'=>false, 'noAuth' => true ), Then when a user logins with Facebook, the plugin sets a PHPSESSID cookie. This PHPSESSID cookie is different to the cookie that my app sets (my cookie has another name). It is like if a session is created and the user is login, but the Session->read(Auth.User) is emtpy. I could not find where in the plugin this cookie is set.

I tried commenting out the session_start() of the Vendor/Facebook.php. This allows me to create users that login with facebook, but the users cannot login afterwards. I don't want to change code in the Facebook SDK anyway.

So the question is, how can I config the plugin to be able to create and login users via the CakePhp Auth and the Facebook Connect? How can I do a logout from my app without kicking the user out of Facebook?

Best regards, David

petehare commented 11 years ago

I'm trying to figure this out as well, any help/answers on the matter would be appreciated!

davidluuAU commented 11 years ago

I have what I believe is a related issue where a user is logged into my web app without Facebook Connect. Then on the same browser the user logs into Facebook. The facebook_id is then written to the user on my web app without going through the Facebook login process on the web app.

andreirebe commented 11 years ago

I need the answer to this question too. I would really appreciate it. Thank you.

petehare commented 11 years ago

@andreirebe I ended up just writing my FB integration from scratch using the Facebook PHP SDK. It ended up working quite well after a fair amount of head scratching.

andreirebe commented 11 years ago

@petehare ar you suggesting that I should do the same? What happens when I have to update it and I do not have the time? Have you thought making a repository with it or share it elsewhere?

petehare commented 11 years ago

@andreirebe It's pretty hard baked into my app, unfortunately I don't have time right now to make it into a module.

Gerifield commented 11 years ago

I found a solution! (I took hours, but it worth!)

My UsersController:

    public function logout(){
        $this->Session->destroy();
        return $this->redirect($this->Auth->logout());
    }

And I modified the the FacebookHelper.php (facebook/View/Helper/FacebookHelper.php) a bit. In the disconnect function I changed the FB.api call, the result:

$onclick = "FB.api({ method: 'Auth.revokeAuthorization' }, function(response) { FB.getLoginStatus(function(response) { ".$response." }); });";

This'll "reload" the login status data before the relocation to the logout page/controller.

You can then use this stuff for the logout:

echo $this->Facebook->disconnect(array('label' => 'Logout', 'redirect' => array('controller' => 'users', 'action' => 'logout')));
andreirebe commented 10 years ago

@Gerifield The problem to your solution is that whenever a users uses the logout function the authorization for the website is deleted.

Gerifield commented 10 years ago

@andreirebe: I know, but at least it works.