1

ユーザーをcakephpでFacebookにログインさせたまま、Facebook APIのサイトからユーザーをログアウトするにはどうすればよいですか? (答えを見つけたので、みんなと共有したかった)。

4

1 に答える 1

3

CakePHP facebook integration logout issue with CakePHP-Facebook-Plugin を読んだ後、私は今これを理解しました。

基本的に、webtechnick の例を使用したデモでは、彼は "Facebook.Connect" コンポーネントを AppController に配置しますが、選択的なログアウト部分が必要な場合は、実際に使用する実際のコントローラー内に配置するのが最適な場所です。それか、AppController に残してnoAuth=> true、Facebook.Connect コンポーネントに渡します。

どちらの方法を選択しても、1 つのコントローラー (facebook_controller.php?) を設定して Facebook のログインを処理し、noauth を false に設定してそのコンポーネントを設定します (これはデフォルトであり、DO 認証を意味します [read connect.php to understand] を意味します)。これ])。そうすれば、ユーザーがいつサイトにログインするかを完全に制御でき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());
        }
        }
}

your "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 に答える