迟到回答,但希望它会帮助你或其他人搜索。
我很难将 HybridAuth 集成到已经使用 Cake Sessions 和 Auth 组件的现有 CakePHP (1.3) 应用程序中。我最初尝试使用从 HybridAuth 网站下载的 Cake 示例,但这对我不起作用。
我们预先存在的用户控制器已经有一堆逻辑,包括一个beforeFilter
方法。这就是我session_start()
在 HybridAuth Cake 示例中的类声明上方插入我的地方:
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();
}
}
}
此时,可以在 Auth 组件中访问第 3 方 (HybridAuth) 用户。这样,用户只登录了一个第 3 方服务或预先存在的登录(但不能同时登录)。
我也有注销的问题。为此,为了安全起见,我基本上销毁了所有会话/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, '/' );
看起来超级hacky - 对不起。希望它有所帮助!