やってること
cakephp3のAuth Componentを用いてログイン認証機能を実装しています。
#困ってること
https://www.ritolab.com/entry/77
上記のサイト通りにログイン機能を実装してみたのですが、ログインができない状態になっています。
よくあるのが「パスワードがハッシュ化されていない」や「DBのpasswordの文字数が少ないからハッシュ化されても途切れてしまっている」などの問題がありますが、それらの問題はおきていません。
passwordもvarchar(255)で設定していますし、DBをみてもpasswordはしっかりとハッシュ化されています。
下記コードにログインに関連数部分を載せますので、回答いただけたら幸いです。
php
1// Controller/UserController.php 2<?php 3namespace App\Controller; 4 5use App\Controller\AppController; 6use Cake\Event\Event; 7 8/** 9 * Users Controller 10 * 11 * @property \App\Model\Table\UsersTable $Users 12 * 13 * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = []) 14 */ 15class UsersController extends AppController 16{ 17 18 /** 19 * 認証スルー設定 20 * @param Event $event 21 * @return \Cake\Http\Response|null|void 22 */ 23 public function beforeFilter(Event $event) 24 { 25 parent::beforeFilter($event); 26 $this->Auth->allow(['add', 'logout']); 27 } 28 29 /** 30 * ログイン 31 * @return \Cake\Http\Response|null 32 */ 33 public function login() 34 { 35 if ($this->request->is('post')) { 36 $user = $this->Auth->identify(); 37 if ($user) { 38 $this->Auth->setUser($user); 39 return $this->redirect($this->Auth->redirectUrl()); 40 } 41 $this->Flash->error(__('ユーザ名もしくはパスワードが間違っています')); 42 } 43 } 44 45 /** 46 * ログアウト 47 * @return \Cake\Http\Response|null 48 */ 49 public function logout() 50 { 51 return $this->redirect($this->Auth->logout()); 52 } 53 54 /** 55 * Index method 56 * 57 * @return \Cake\Http\Response|void 58 */ 59 public function index() 60 { 61 $users = $this->paginate($this->Users); 62 63 $this->set(compact('users')); 64 } 65 66 /** 67 * View method 68 * 69 * @param string|null $id User id. 70 * @return \Cake\Http\Response|void 71 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 72 */ 73 public function view($id = null) 74 { 75 $user = $this->Users->get($id, [ 76 'contain' => ['Foods', 'Participants'] 77 ]); 78 79 $this->set('user', $user); 80 } 81 82 /** 83 * Add method 84 * 85 * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. 86 */ 87 public function add() 88 { 89 $user = $this->Users->newEntity(); 90 if ($this->request->is('post')) { 91 $user = $this->Users->patchEntity($user, $this->request->getData()); 92 if ($this->Users->save($user)) { 93 $this->Flash->success(__('The user has been saved.')); 94 95 return $this->redirect(['action' => 'index']); 96 } 97 $this->Flash->error(__('The user could not be saved. Please, try again.')); 98 } 99 $this->set(compact('user')); 100 } 101 102 /** 103 * Edit method 104 * 105 * @param string|null $id User id. 106 * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. 107 * @throws \Cake\Network\Exception\NotFoundException When record not found. 108 */ 109 public function edit($id = null) 110 { 111 $user = $this->Users->get($id, [ 112 'contain' => [] 113 ]); 114 if ($this->request->is(['patch', 'post', 'put'])) { 115 $user = $this->Users->patchEntity($user, $this->request->getData()); 116 if ($this->Users->save($user)) { 117 $this->Flash->success(__('The user has been saved.')); 118 119 return $this->redirect(['action' => 'index']); 120 } 121 $this->Flash->error(__('The user could not be saved. Please, try again.')); 122 } 123 $this->set(compact('user')); 124 } 125 126 /** 127 * Delete method 128 * 129 * @param string|null $id User id. 130 * @return \Cake\Http\Response|null Redirects to index. 131 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 132 */ 133 public function delete($id = null) 134 { 135 $this->request->allowMethod(['post', 'delete']); 136 $user = $this->Users->get($id); 137 if ($this->Users->delete($user)) { 138 $this->Flash->success(__('The user has been deleted.')); 139 } else { 140 $this->Flash->error(__('The user could not be deleted. Please, try again.')); 141 } 142 143 return $this->redirect(['action' => 'index']); 144 } 145} 146
php
1// Controller/AppController.php 2<?php 3namespace App\Controller; 4 5use Cake\Controller\Controller; 6use Cake\Event\Event; 7 8class AppController extends Controller 9{ 10 11 public function initialize() 12 { 13 parent::initialize(); 14 15 $this->loadComponent('RequestHandler'); 16 $this->loadComponent('Flash'); 17 $this->loadComponent('Auth', [ 18 'loginAction' => [ 19 'controller' => 'Users', 20 'action' => 'login' 21 ], 22 'loginRedirect' => [ 23 'controller' => 'Users', 24 'action' => 'index' 25 ], 26 'logoutRedirect' => [ 27 'controller' => 'Users', 28 'action' => 'login', 29 'home' 30 ], 31 'authenticate' => [ 32 'Form' => [ 33 'fields' => ['username' => 'email', 'password' => 'pass'] 34 ] 35 ], 36 ]); 37 } 38 39 // 認証を通さないアクションがある場合のみ 40 public function beforeFilter(Event $event) 41 { 42 //$this->Auth->allow(['add']); 43 } 44 45} 46
php
1// Model/Entity/User.php 2<?php 3namespace App\Model\Entity; 4 5use Cake\ORM\Entity; 6use Cake\Auth\DefaultPasswordHasher; 7 8class User extends Entity 9{ 10 protected $_accessible = [ 11 '*' => true, 12 'id' => false 13 ]; 14 15 /** 16 * Fields that are excluded from JSON versions of the entity. 17 * 18 * @var array 19 */ 20 protected $_hidden = [ 21 'password' 22 ]; 23 24 25 protected function _setPassword($password) 26 { 27 if (strlen($password) > 0) { 28 return (new DefaultPasswordHasher)->hash($password); 29 } 30 } 31} 32
php
1// Model/Table/UsersTable.php 2<?php 3namespace App\Model\Table; 4 5use Cake\ORM\Query; 6use Cake\ORM\RulesChecker; 7use Cake\ORM\Table; 8use Cake\Validation\Validator; 9 10class UsersTable extends Table 11{ 12 13 public function initialize(array $config) 14 { 15 parent::initialize($config); 16 17 $this->setTable('users'); 18 $this->setDisplayField('id'); 19 $this->setPrimaryKey('id'); 20 21 $this->addBehavior('Timestamp'); 22 23 $this->hasMany('Foods', [ 24 'foreignKey' => 'user_id' 25 ]); 26 $this->hasMany('Participants', [ 27 'foreignKey' => 'user_id' 28 ]); 29 } 30 31 public function validationDefault(Validator $validator) 32 { 33 $validator 34 ->scalar('password') 35 ->maxLength('password', 255) 36 ->requirePresence('password', 'create') 37 ->notEmpty('password'); 38 39 $validator 40 ->email('email') 41 ->requirePresence('email', 'create') 42 ->notEmpty('email'); 43 44 return $validator; 45 } 46 47 public function buildRules(RulesChecker $rules) 48 { 49 $rules->add($rules->isUnique(['username'])); 50 $rules->add($rules->isUnique(['email'])); 51 52 return $rules; 53 } 54} 55
回答1件
あなたの回答
tips
プレビュー