CakephpのAuthを使ってログインが出来ません!
前は同じコードでできたような気がするのですが、今回はできません。
なぜでしょうか?
login.ctp
lang
1<?php 2 echo $this->Form->create('User'); 3 echo $this->Form->input('User.username'); 4 echo $this->Form->input('User.password'); 5 echo $this->Form->end('ログインする'); 6 ?>
UsersController.php
lang
1class UsersController extends AppController { 2 public $helpers = array("Html",'Form'); 3 public $uses = array('User'); 4 public $components = array('Auth'); 5 public function index(){ 6 7 } 8 9 public function login() { 10 // debug($this->User->find('all')); 11 if($this->request->is('post')) { 12 debug($this->request->data); 13 debug($this->Auth->login()); 14 if($this->Auth->login()) { 15 debug('success'); 16 return $this->redirect(array('controller'=>'posts','action'=>'index')); 17 } else { 18 debug('faild'); 19 $this->Session->setFlash('ログインに失敗しました'); 20 } 21 } 22 } 23 24 public function logout() { 25 $this->Auth->logout(); 26 $this->redirect('login'); 27 } 28 29 public function beforeFilter() { 30 parent::beforeFilter(); 31 $this->Auth->allow('login','add'); 32 } 33 34 public function add() { 35 if ($this->request->is('post')) { 36 $this->User->create(); 37 if ($this->User->save($this->request->data)) { 38 $this->Session->setFlash(__('The user has been saved')); 39 $this->Auth->login(); 40 $this->redirect(array('controller'=>'posts','action' => 'index')); 41 } else { 42 $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 43 } 44 } 45 } 46}
User.php
lang
1<?php 2 3class User extends AppModel { 4 public $name = 'User'; 5 // public $belongsTo = array(''); 6 public $hasMany = array('Post'); 7 var $validate = array( 8 'username' => array( 9 'notempty' => array( 10 'rule' => array('notempty'), // 未入力チェック 11 ), 12 'isUnique' => array( 13 'rule' => 'isUnique', // 重複チェック 14 ), 15 ), 16 'password' => array( 17 'custom' => array( 18 'rule' => array('custom', '/^[a-zA-Z0-9]{6,}$/i'), // 半角英数6文字以上 19 ), 20 ), 21 ); 22 23 public function beforeSave($options = array()) { 24 $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); 25 // $this->data['User']['password'] = sha1($this->data['User']['password']); 26 return true; 27 28 } 29}
AppController.php
lang
1class AppController extends Controller { 2 public $components = array( 3 'Session', 4 'Auth' 5 ); 6} 7
ちなみにUsersControllerのaddでユーザーを新規追加した場合、そのままログインできます。なのでadd()の$this->Auth->login()はうまくいくのですが、login()の$this->Auth->login()がうまくいかないということです。
パスワードの暗号化もデータベースで確認できています。
おそらく、ログインフォームで入力したパスワードとデータベースのパスワードが一致していないと思うのですが、どうでしょうか??
助けてもらえると嬉しいです・・・!

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。