CakePhp3でAuthComponentを使用してログイン認証を行いたいのです。
下記のコードでログイン画面は表示されるのですが、ログインができません。
どこが悪い、またはどこが足りないのかご教授いただけないでしょうか。
・テーブル
sql
1CREATE TABLE `users` ( 2 `id` int(10) UNSIGNED NOT NULL, 3 `username` varchar(50) DEFAULT NULL, 4 `password` varchar(255) DEFAULT NULL, 5 `role` varchar(20) DEFAULT NULL, 6 `created` datetime DEFAULT NULL, 7 `modified` datetime DEFAULT NULL 8) ENGINE=InnoDB DEFAULT CHARSET=utf8;
・appController.php
php
1 public function initialize() 2 { 3 parent::initialize(); 4 5 $this->loadComponent('Flash'); // Flashコンポーネント。エラーメッセージの表示などに使用 6 $this->loadComponent('RequestHandler'); // RequestHandlerコンポーネント。入力されたデータの取得などに使用 7 $this->loadComponent('Auth', [ 8 'loginAction' => [ 9 'controller' => 'Users', 10 'action' => 'login', 11 ], 12 'authorize' => ['Controller'], 13 'authError' => 'ログインしてください', 14 'authenticate' => [ 15 'Form' => [ 16 'fields' => ['username' => 'username', 17 'password' => 'password' 18 ] 19 ] 20 ], 21 'loginRedirect' => ['controller' => 'Users', 22 'action' => 'index'], 23 'logoutRedirect' => [ 24 'controller' => 'Users', 25 'action' => 'login', 26 ] 27 28 ]);
・UsersTable.php
php
1 2namespace App\Model\Table; 3 4use Cake\ORM\Query; 5use Cake\ORM\RulesChecker; 6use Cake\ORM\Table; 7use Cake\Validation\Validator; 8 9/** 10 * Users Model 11 * 12 * @method \App\Model\Entity\User get($primaryKey, $options = []) 13 * @method \App\Model\Entity\User newEntity($data = null, array $options = []) 14 * @method \App\Model\Entity\User[] newEntities(array $data, array $options = []) 15 * @method \App\Model\Entity\User|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) 16 * @method \App\Model\Entity\User patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) 17 * @method \App\Model\Entity\User[] patchEntities($entities, array $data, array $options = []) 18 * @method \App\Model\Entity\User findOrCreate($search, callable $callback = null, $options = []) 19 * 20 * @mixin \Cake\ORM\Behavior\TimestampBehavior 21 */ 22class UsersTable extends Table 23{ 24 25 /** 26 * Initialize method 27 * 28 * @param array $config The configuration for the Table. 29 * @return void 30 */ 31 public function initialize(array $config) 32 { 33 parent::initialize($config); 34 35 $this->setTable('users'); 36 $this->setDisplayField('id'); 37 $this->setPrimaryKey('id'); 38 39 $this->addBehavior('Timestamp'); 40 } 41 42 /** 43 * Default validation rules. 44 * 45 * @param \Cake\Validation\Validator $validator Validator instance. 46 * @return \Cake\Validation\Validator 47 */ 48 public function validationDefault(Validator $validator) 49 { 50 $validator 51 ->integer('id') 52 ->allowEmpty('id', 'create'); 53 54 $validator 55 ->allowEmpty('username'); 56 57 $validator 58 ->allowEmpty('password'); 59 60 $validator 61 ->allowEmpty('role'); 62 63 return $validator; 64 } 65 66 /** 67 * Returns a rules checker object that will be used for validating 68 * application integrity. 69 * 70 * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. 71 * @return \Cake\ORM\RulesChecker 72 */ 73 public function buildRules(RulesChecker $rules) 74 { 75 $rules->add($rules->isUnique(['username'])); 76 77 return $rules; 78 } 79} 80
・User.php(Entity)
php
1<?php 2namespace App\Model\Entity; 3 4use Cake\ORM\Entity; 5 6/** 7 * User Entity 8 * 9 * @property int $id 10 * @property string $username 11 * @property string $password 12 * @property string $role 13 * @property \Cake\I18n\FrozenTime $created 14 * @property \Cake\I18n\FrozenTime $modified 15 */ 16class User extends Entity 17{ 18 19 /** 20 * Fields that can be mass assigned using newEntity() or patchEntity(). 21 * 22 * Note that when '*' is set to true, this allows all unspecified fields to 23 * be mass assigned. For security purposes, it is advised to set '*' to false 24 * (or remove it), and explicitly make individual fields accessible as needed. 25 * 26 * @var array 27 */ 28 protected $_accessible = [ 29 '*' => true, 30 'id' => false 31 ]; 32 33 /** 34 * Fields that are excluded from JSON versions of the entity. 35 * 36 * @var array 37 */ 38 protected $_hidden = [ 39 'password' 40 ]; 41 42protected function _setPassword($password) 43 { 44 return (new DefaultPasswordHasher)->hash($password); 45 } 46} 47
・UsersController.php
php
1 2 function login(){ 3 if($this->request->isPost()){ 4 $user = $this->Auth->identify(); 5 if(!empty($user)){ 6 $this->Auth->setUser($user); 7 return $this->redirect($this->Auth->redirectUrl()); 8 } 9 $this->Flash->error('ユーザー名かパスワードが間違っています。'); 10 } 11 }
・login.ctp
php
1<div class="users form"> 2<?= $this->Flash->render('auth') ?> 3<?= $this->form->create() ?> 4 <fieldset> 5 <legend>アカウント名とパスワードを入力ください</legend> 6 <?= $this->Form->input('username'); ?> 7 <?= $this->Form->input('password'); ?> 8 </fieldset> 9<?= $this->Form->button(__('送信')); ?> 10<?= $this->Form->end(); ?> 11</div>
となっております。
よろしくお願いいたします。
usersには正しくデータが入っているんですよね?ハッシュされたパスワードも。
申し訳ありません。パスワードはハッシュされたものでないとダメなのですか?
_setPassword()にてDefaultPasswordHasherを利用されてますよね。入力パスワードをハッシュしてUser問い合わせしているように見えるので、データベース内のpasswordも当然ハッシュされてないと合致しないのでは、と思うのですが。
説明ありがとうございます。_setPassword()を外してみたのですが、変わらず、ほかに何か原因はあるでしょうか?
え、いや、外すのではなく、データベースのusersテーブルのpasswordの保存値をハッシュ化すべきという話で・・・
あなたの回答
tips
プレビュー