1

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

4

1 回答 1

3

在阅读CakePHP facebook integration logout issue with CakePHP-Facebook-Plugin之后,我刚刚想到了这一点。

基本上,虽然在带有 webtechnick 示例的演示中,他将“Facebook.Connect”组件放在 AppController 中,但如果您想要选择性注销部分,最好的放置位置实际上是在您想要使用它的实际控制器中in. 或者将其留在 AppController 中并传递noAuth=> true给 Facebook.Connect 组件。

无论哪种方式,无论您选择哪种方式,您都设置了一个控制器(facebook_controller.php?)来处理 facebook 登录,并将其组件设置为 noauth 设置为 false(这是默认设置,意味着 DO 验证 [阅读 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 extends AppController { ... // 我个人不喜欢让他的作品创建我的用户,所以: var $components = array('Facebook.Connect' => array('createUser'=>错误的)); ...

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'))); ?>

这应该差不多了。我希望这对阅读本文但仍需要帮助的人有所帮助。

于 2012-06-14T09:25:34.313 回答