質問編集履歴

2

ミス

2015/07/07 02:00

投稿

Shinog
Shinog

スコア99

test CHANGED
@@ -1 +1 @@
1
- このCekePHPコードの何が違ますか??
1
+ CekePHPにおけるFcebookログインにつて!!
test CHANGED
File without changes

1

ミス

2015/07/07 02:00

投稿

Shinog
Shinog

スコア99

test CHANGED
File without changes
test CHANGED
@@ -8,196 +8,8 @@
8
8
 
9
9
  この記事通りに展開したところ、Facebook上の情報をMySQLに格納は出来たものの、ログイン状態にならないことが分かりました。
10
10
 
11
- この記事のlogin関数が少々乱雑だと感じたため、以下のようなコードに変更してみたのですが、それでもログイン状態になりません。
12
-
13
11
 
14
12
 
15
13
  どうすればログイン状態にすることができるでしょうか??
16
14
 
17
15
  何かお分かりの方は是非ご教授の程よろしくお願い申し上げます。
18
-
19
- ```lang-php
20
-
21
- <?php
22
-
23
- //UsersController.php
24
-
25
- App::uses('AppController', 'Controller');
26
-
27
- App::import('vendor', 'facebook/src/facebook');
28
-
29
-
30
-
31
- class UsersController extends AppController {
32
-
33
-
34
-
35
-
36
-
37
- public $helpers = array('Form', 'Html');
38
-
39
- public $components = array(
40
-
41
- 'Auth' => array(
42
-
43
- 'loginRedirect' => array('controller' => 'shops', 'action' => 'index'),
44
-
45
- 'logoutRedirect' => array('controller' => 'shops', 'action' => 'index'),
46
-
47
- ),
48
-
49
- 'Session',
50
-
51
- );
52
-
53
-
54
-
55
-
56
-
57
- public $Facebook;
58
-
59
-
60
-
61
- public function beforeFilter() {
62
-
63
- $this->Facebook = new Facebook(array(
64
-
65
- 'appId' => '省略',
66
-
67
- 'secret' => '省略',
68
-
69
- 'cookie' => true,
70
-
71
- ));
72
-
73
- $this->Auth->allow('login', 'logout');
74
-
75
- }
76
-
77
-
78
-
79
- public function index() {
80
-
81
- if ($this->Auth->loggedIn()) {
82
-
83
- $facebookId = $this->Facebook->getUser();
84
-
85
- $this->set('user', $this->User->find('first', ['conditions' => ['User.id' => $facebookId]]));
86
-
87
- } else {
88
-
89
- $this->redirect(['action' => 'login']);
90
-
91
- }
92
-
93
- }
94
-
95
-
96
-
97
- public function login() {
98
-
99
- $this->autoRender = false;
100
-
101
- // facebook OAuth login
102
-
103
- $facebookId = $this->Facebook->getUser();
104
-
105
- $user = $this->User->find('first', ['conditions' => ['User.id' => $facebookId]]);
106
-
107
- if (isset($user['User'])) {
108
-
109
- if ($this->Auth->login($user)) {
110
-
111
- return $this->redirect(['action' => 'index']);
112
-
113
- } else {
114
-
115
- return $this->_add();
116
-
117
- }
118
-
119
- }
120
-
121
- }
122
-
123
-
124
-
125
- protected function _authFacebook() {
126
-
127
- $loginUrl = $this->Facebook->getLoginUrl(['redirect_uri' => FULL_BASE_URL . Router::url(['controller' => 'users', 'action' => 'login'])]);
128
-
129
- return $this->redirect($loginUrl);
130
-
131
- }
132
-
133
-
134
-
135
- public function logout() {
136
-
137
- $logout_url = $this->Auth->logout();
138
-
139
- $this->redirect($logout_url);
140
-
141
- }
142
-
143
-
144
-
145
- protected function _add() {
146
-
147
- $this->autoRender = false;
148
-
149
-
150
-
151
- $facebookInfo = $this->Facebook->api('/me', 'GET');
152
-
153
- $user = array(
154
-
155
- 'User' => [
156
-
157
- 'id' => $facebookInfo['id'],
158
-
159
- 'name' => $facebookInfo['name'],
160
-
161
- /** createdとmodifiedというカラムがある場合、
162
-
163
- .*. 下記のように記述しなくても、作成時と更新時に自動で現在時刻が挿入される。
164
-
165
- .*. よって'created'、'modified'は削除してもOK
166
-
167
- .*/
168
-
169
- 'created' => date('Y-m-d H:i:s'),
170
-
171
- 'modified' => date('Y-m-d H:i:s'),
172
-
173
- 'link' => $facebookInfo['link'],
174
-
175
- ]
176
-
177
- );
178
-
179
- $this->User->create();
180
-
181
- if ($this->User->save($user)) {
182
-
183
- $this->Session->setFlash(__('登録が完了しました。'));
184
-
185
- } else {
186
-
187
- $this->Session->setFlash(__('登録てきません.'));
188
-
189
- }
190
-
191
-
192
-
193
- $this->redirect(['action' => 'index']);
194
-
195
- }
196
-
197
-
198
-
199
- }
200
-
201
-
202
-
203
- ```