回答が遅くなりましたが、あなたや他の誰かが検索するのに役立つことを願っています.
すでに Cake セッションと Auth コンポーネントを使用している既存の CakePHP (1.3) アプリに HybridAuth を統合するのに苦労しました。最初に、HybridAuth Web サイトからダウンロードした Cake の例を使用してみましたが、うまくいきませんでした。
既存の User コントローラーには、メソッドを含む一連のロジックが既にありましたbeforeFilter
。session_start()
HybridAuth Cake の例のクラス宣言の上ではなく、 my を挿入した場所です。
class UsersController extends AppController {
...
var $components = array('Session','Auth','Email');
...
public function beforeFilter(){
// following two lines may not be necessary for your setup
$this->Auth->allow('oauth', 'oauthLogout');
$excludeBeforeFilter = array('oauth', 'oauthLogout');
if ( ! in_array($this->action, $excludeBeforeFilter) ) {
// preexisting Auth logic -- I had to bypass it because
// calling `session_start` was messing up Cake's Auth
...
} else {
/* THIS IS NECCESSARY FOR HYBRIDAUTH (OAUTH) */
/* setting the session ID made it so users could NOT log
in from a 3rd party and our site */
session_id($_COOKIE['CAKEPHP']);
session_start();
}
}
...
public function oauth($provider){
$this->autoRender = false;
// include HybridAuth
require_once( WWW_ROOT . 'hybridauth/Hybrid/Auth.php' );
try {
// I stored my provider config in a Cake config file
// It's essentially a copy from the site/Cake example
$hybridAuth = new Hybrid_Auth(Configure::read('HAuth.config'));
$adapter = $hybridAuth->authenticate($provider);
$userProfile = $adapter->getUserProfile();
// method on the User model to find or create the 3rd party user
$user = $this->User->findOrCreate($userProfile, $provider);
if ( ! $user ) {
echo 'Unable to find/create user';
} else {
// configure your fields as necessary
$this->Auth->fields = array(...);
$this->Auth->login($user);
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
}
この時点で、サードパーティ (HybridAuth) ユーザーは認証コンポーネントでアクセスできます。このようにして、ユーザーは 1 つのサード パーティ サービスまたは既存のログイン (両方ではなく) のみにログインしました。
ログアウトの問題もありました。そのために、基本的に安全のためにすべてのセッション/Cookieを破棄しました。のoauthLogout
方法ではUsersController
:
// probably overkill/paranoia
$this->Auth->logout();
$this->Session->destroy();
session_destroy($_COOKIE['CAKEPHP']);
/* these two seemed to make the difference
especially the 4th parameter */
setcookie( 'PHPSESSID', '', 1, '/' );
setcookie( 'CAKEPHP', '', 1, '/' );
非常にハッキーなようです-申し訳ありません。それが役立つことを願っています!