如何在保存facebook登录的同时使用facebook连接在cakephp中实现注销?

时间:2012-06-14 09:25:34

标签: facebook cakephp

如何在facebook api中从您的网站注销用户,同时仍然让用户使用cakephp登录facebook? (我找到答案,希望与大家分享)。

1 个答案:

答案 0 :(得分:3)

在阅读CakePHP facebook integration logout issue with CakePHP-Facebook-Plugin之后,我刚才想出了这个。

基本上,虽然在使用webtechnick的例子的演示中,他提出了" Facebook.Connect" AppController中的组件,如果你想要选择性注销片段,最好把它放在你想要使用它的实际控制器中。或者把它放在AppController中并将noAuth=> true传递给Facebook .Connect组件。

无论哪种方式,无论你选择哪种方式,你都设置一个控制器(facebook_controller.php?)来处理facebook登录,并设置其组件,noauth设置为false(默认情况下,这意味着DO认证[read connect]。 php了解这个])。这样,您就可以完全控制用户何时登录到站点,并且您可以实际将其注销(使用常规redirect($this->Auth->logout()),而无需使用连接组件立即将其重新登录。以下是一个实现:

让我给你一个想法:

app_controller.php

class AppController extends Controller {
    var $components = array('Auth', 'Acl', 'Session');
       //or if you want access to "$this->Connect" universally:
       //   array('Auth', 'Facebook.Connect' => 
       //                      array('noauth'=>'true'), 'Acl', 'Session');
}

users_controller.php中:

class UsersController extends AppController{
var $helpers = array('Facebook.Facebook');
        //an example of the users controller, enabling connect, but
        // not authorizing the user (because logout() used by Auth is here)
    var $components = array('Email', 'Session', 'Facebook.Connect' => array('createUser'=>false, 'noauth'=>true));

        //login() doesnt need to be shown and can be left alone for your traditional users

        function logout(){
              //if there is no fb user, do the logout normal
              if ($this->Connect->FB->getUser() == 0){
                    $this->redirect($this->Auth->logout());
        }else{
                //ditch FB data for safety
                $this->Connect->FB->destroysession();
                //hope its all gone with this
        session_destroy();
                //logout and redirect to the screen that you usually do.
        $this->redirect($this->Auth->logout());
        }
        }
}

你的" facebook_controller.php":     class FacebookaController扩展AppController {     ...     //我个人不喜欢让他的作品创造我的用户:     var $ components = array(' Facebook.Connect' =>数组(' createUser' => false));     ...

function login(){
//just need this stub function for later
$this->autoRender = false;
}

//you also need this for deauths or they will still be able to get into the site after deauth (against policy or whatever)
    function deauthorize(){
    //get user id from facebook API
    $uid = $this->Connect->FB->getUser();
    $record = $this->User->findByFacebookId($uid);
    $this->User->delete($record['id'], FALSE);  
    }
}

现在是您的users / login.ctp文件:

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'your app id', // App ID
      channelUrl : '//'+window.location.hostname+'/facebook/channel', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

    // Additional initialization code here
    FB.Event.subscribe('auth.statusChange', function(response){
        if (response.status == "connected"){
            alert('redirecting you to auto facebook login');
                //here is out default place for login
            window.location.href = "http://"+window.location.hostname + "/facebook/login";
        }
    });
  };

  // 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.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));
</script>
<?php e($this->Facebook->login(array('registration-url'=>'http://www.yoursite.com/facebook/signup'))); ?>

这应该是它。我希望这可以帮助那些仍然需要帮助的人阅读。