cakephp2系で、google plusのoauth認証をしたいと思ったポリドッグです。
cakephp2系からAuthコンポーネントの拡張が柔軟になったし、AuthComponent使った形で実装してみました。
ソースコードはこちら
https://github.com/polidog/GooglePlusComponent
使用例の参考のコントローラはこんな感じです。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<?php
/**
* 認証用コントローラ
*/
class AuthController extends AppController
{
public $components = array(
'Auth' => array(
'authenticate' => array(
'GooglePlus',
),
'authorize' => array(
'GooglePlus',
),
'loginAction' => array(
'controller' => 'auth',
'action' => 'login'
),
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'index'
),
),
'GooglePlus' => array(
'clientId' => 'クライアントID',
'clientSecret' => 'シークレット',
'auth' => array(
'callback' => 'http://localhost/test/callback',
),
'scope' => array(
'https://www.googleapis.com/auth/plus.me',
),
)
);
public function beforeFilter() {
parent::beforeFilter();
}
public function index() {
// 認証しているかどうかチェックする
debug($this->Auth->isAuthorized());
}
public function login() {
// ログイン処理
$this->Auth->login();
}
public function logout() {
// ログアウト処理
$this->Auth->logout();
$this->redirect('/');
}
}
|
google-api-php-client使えよって突っ込みはなしでお願いします・・・