cakephpにてログインがうまくいかない

実現したいこと

初めまして。
現在cakephpの勉強中で、ログイン部分で躓いてしまいました。。
DBに登録されているemailとpasswordでログインできるようにしたいです。

発生している問題・分からないこと

DBに登録されているはずのユーザーでログインができず、
「もう一度ログインしてください。」という任意のエラーメッセージが出てしまい、ログイン画面にリダイレクトされます。。

UserControllerにて記載している以下の「postできたデータをDBと照らし合わせる」コードで
$userが空で返ってきてしまうため、上記のメッセージがでます。
$user = $this->Auth->identify();

エラーメッセージ

error

1cakephpからエラーは返ってきませんが、認証できなかったときのエラーメッセージが返ってきてしまいます。 2 3 if ($user) { 4 $this->Auth->setUser($user); 5 return $this->redirect($this->Auth->redirectUrl()); 6 } 7 $this->Flash->error(__('もう一度ログインしてください。')); ←ここに行ってしまう!!!!! 8

該当のソースコード

AppController

1class AppController extends Controller 2{ 3 public function initialize():void 4 { 5 parent::initialize(); 6 7 $this->loadComponent('RequestHandler'); 8 $this->loadComponent('Flash'); 9 10 $this->loadComponent('Auth', [ 11 'loginRedirect' => [ 12 'controller' => 'Users', 13 'action' => 'index' 14 ], 15 'logoutRedirect' => [ 16 'controller' => 'Users', 17 'action' => 'login' 18 ], 19 'authenticate' => [ 20 'Form' => [ 21 'userModel' => 'Users', 22 'fields' => [ 23 'username' => 'mail', 24 'password' => 'password' 25 ] 26 ] 27 ], 28 ]); 29 30 $this->Auth->allow(['login','add']); 31 $this->loadComponent('Security'); 32 } 33}

UsersController

1<?php 2namespace App\Controller; 3 4use App\Controller\AppController; 5use Cake\Log\Log; 6/** 7 * Users Controller 8 * 9 * @property \App\Model\Table\UsersTable $Users 10 * 11 * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = []) 12 */ 13class UsersController extends AppController 14{ 15 /** 16 * Index method 17 * 18 * @return \Cake\Http\Response|void 19 */ 20 public function index() 21 { 22 $users = $this->paginate($this->Users); 23 24 $this->set(compact('users')); 25 } 26 27 /** 28 * View method 29 * 30 * @param string|null $id User id. 31 * @return \Cake\Http\Response|void 32 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 33 */ 34 public function view($id = null) 35 { 36 $user = $this->Users->get($id, [ 37 'contain' => [] 38 ]); 39 40 $this->set('user', $user); 41 } 42 43 /** 44 * Add method 45 * 46 * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. 47 */ 48 public function add() 49 { 50 $user = $this->Users->newEntity(); 51 if ($this->request->is('post')) { 52 $user = $this->Users->patchEntity($user, $this->request->getData()); 53 if ($this->Users->save($user)) { 54 $this->Flash->success(__('The user has been saved.')); 55 56 return $this->redirect(['action' => 'index']); 57 } 58 $this->Flash->error(__('The user could not be saved. Please, try again.')); 59 } 60 $this->set(compact('user')); 61 } 62 63 /** 64 * Edit method 65 * 66 * @param string|null $id User id. 67 * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. 68 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 69 */ 70 public function edit($id = null) 71 { 72 $user = $this->Users->get($id, [ 73 'contain' => [] 74 ]); 75 if ($this->request->is(['patch', 'post', 'put'])) { 76 $user = $this->Users->patchEntity($user, $this->request->getData()); 77 if ($this->Users->save($user)) { 78 $this->Flash->success(__('The user has been saved.')); 79 80 return $this->redirect(['action' => 'index']); 81 } 82 $this->Flash->error(__('The user could not be saved. Please, try again.')); 83 } 84 $this->set(compact('user')); 85 } 86 87 /** 88 * Delete method 89 * 90 * @param string|null $id User id. 91 * @return \Cake\Http\Response|null Redirects to index. 92 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 93 */ 94 public function delete($id = null) 95 { 96 $this->request->allowMethod(['post', 'delete']); 97 $user = $this->Users->get($id); 98 if ($this->Users->delete($user)) { 99 $this->Flash->success(__('The user has been deleted.')); 100 } else { 101 $this->Flash->error(__('The user could not be deleted. Please, try again.')); 102 } 103 104 return $this->redirect(['action' => 'index']); 105 } 106 107 108 109 # ログイン画面 110 public function login() 111 { 112 113 114 if ($this->request->is('post')) { 115 116 $username = $this->request->getData('username'); 117 $password = $this->request->getData('password'); 118 119 $user = $this->Auth->identify(); 120 121 Log::debug('変数$nameの値は:'. $username . 'です。'); 122 Log::debug('変数$nameの値は:'. $password . 'です。'); 123 Log::debug('変数$nameの値は:'. $user . 'です。'); 124 125 if ($user) { 126 $this->Auth->setUser($user); 127 return $this->redirect($this->Auth->redirectUrl()); 128 } 129 130 $this->Flash->error(__('もう一度ログインしてください。')); 131 132 } 133 } 134 public function logout() 135 { 136 return $this->redirect($this->Auth->logout()); 137 } 138 139}

UsersTable

1<?php 2declare(strict_types=1); 3 4namespace App\Model\Table; 5 6use Cake\ORM\RulesChecker; 7use Cake\ORM\Table; 8use Cake\Validation\Validator; 9 10/** 11 * Users Model 12 * 13 * @property \App\Model\Table\ArticlesTable&\Cake\ORM\Association\HasMany $Articles 14 * 15 * @method \App\Model\Entity\User newEmptyEntity() 16 * @method \App\Model\Entity\User newEntity(array $data, array $options = []) 17 * @method \App\Model\Entity\User[] newEntities(array $data, array $options = []) 18 * @method \App\Model\Entity\User get($primaryKey, $options = []) 19 * @method \App\Model\Entity\User findOrCreate($search, ?callable $callback = null, $options = []) 20 * @method \App\Model\Entity\User patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) 21 * @method \App\Model\Entity\User[] patchEntities(iterable $entities, array $data, array $options = []) 22 * @method \App\Model\Entity\User|false save(\Cake\Datasource\EntityInterface $entity, $options = []) 23 * @method \App\Model\Entity\User saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = []) 24 * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable $entities, $options = []) 25 * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = []) 26 * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = []) 27 * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = []) 28 * 29 * @mixin \Cake\ORM\Behavior\TimestampBehavior 30 */ 31class UsersTable extends Table 32{ 33 /** 34 * Initialize method 35 * 36 * @param array $config The configuration for the Table. 37 * @return void 38 */ 39 public function initialize(array $config): void 40 { 41 parent::initialize($config); 42 43 $this->setTable('users'); 44 $this->setDisplayField('username'); 45 $this->setPrimaryKey('No'); 46 47 $this->addBehavior('Timestamp'); 48 49 } 50} 51

User(Entity)

1<?php 2namespace App\Model\Entity; 3 4use Cake\ORM\Entity; 5use Cake\Auth\DefaultPasswordHasher; 6 7class User extends Entity 8{ 9 # Noは変更不可、それ以外は変更可能 10 protected $_accessible = [ 11 '*' => true, 12 'No' => false 13 ]; 14 protected $_hidden = [ 15 'password' 16 ]; 17 18 protected function _setPassword($password) 19 { 20 return (new DefaultPasswordHasher)->hash($password); 21 } 22 23}

login.php

試したこと・調べたこと

上記の詳細・結果

ネットで調べ、いろいろ試してみたのですがうまく行かずです。。

補足

特になし

コメントを投稿

0 コメント