CakePHP2.6.10によりログイン画面の実装を試みているのですが、登録したユーザでログインしようとしてもログインすることができません。
どのように対処すれば宜しいでしょうか。どうか宜しくお願いいたします。
以下、ソースコードとなります。
PHP
1<?php 2 3App::uses('AppController', 'Controller'); 4 5class UsersController extends AppController { 6 public $name = 'Users'; 7 public $components = Array('Session','Auth'); 8 9 public function login() { 10 if ($this->request->is('post')) { 11 if ($this->Auth->login()) {//Does not work well 12 echo "Hello, world"; 13 $this->redirect($this->Auth->redirectUrl()); 14 } 15 else { 16 echo "Error"; 17 $this->Session->setFlash('Wrong email address or password', 'default', array(), 'auth'); 18 } 19 } 20 } 21 public function logout() { 22 $this->Auth->logout(); 23 //turn $this->redirect(array('action'=>'index', 'controller'=>'Members')); 24 } 25 26 public function add() { 27 if(!empty($this->data)) { 28 if ($this->data) { 29 $this->User->create(); 30 $this->User->save($this->data); 31 $this->redirect(array('action' => 'login')); 32 } 33 } 34 } 35 36 public function beforeFilter() { 37 $this->Auth->allow('add','logout'); 38 } 39}
PHP
1//Model/User.php 2App::uses('AppModel', 'Model'); 3App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth'); 4 5class User extends AppModel { 6 7 public function beforeSave($options = array()) { 8 9 if (isset($this->data[$this->alias]['password'])) { 10 $passwordHasher = new BlowfishPasswordHasher(); 11 $this->data[$this->alias]['password'] = $passwordHasher->hash( 12 $this->data[$this->alias]['password'] 13 ); 14 } 15 return true; 16 } 17 18}
PHP
1 2// Controller/AppController.php 3App::uses('Controller', 'Controller'); 4 5Configure::write('debug', 1); 6 7class AppController extends Controller { 8 9 public $components = array( 10 'Auth' => array( 11 'loginAction' => array( 12 'controller' => 'users', 13 'action' => 'login' 14 ), 15 'loginRedirect' => array( 16 'controller' => 'members', 17 'action' => 'index' 18 ), 19 'logoutRedirect' => array( 20 'controller' => 'mains', 21 'action' => 'index', 22 'home' 23 ), 24 25 'authenticate' => array( 26 'Form' => array( 27 'passwordHasher' => 'Blowfish', 28 'hashType' => 'sha1' 29 ) 30 ) 31 32 ) 33 ); 34 35 public function beforeFilter() { 36 $this->Auth->allow('index'); 37 } 38} 39
PHP
1// View/Users/add.ctp 2<div class="users form"> 3<?php echo $this->Form->create('User'); ?> 4 <fieldset> 5 <legend><?php echo __('Add User'); ?></legend> 6 <?php 7 echo $this->Form->input('name'); 8 echo $this->Form->input('mail_address'); 9 echo $this->Form->input('password'); 10 ?> 11 </fieldset> 12<?php echo $this->Form->end(__('Registration')); ?> 13</div>
PHP
1// View/Users/login.ctp 2<div class="users form"> 3<?php echo $this->Form->create('User'); ?> 4 <fieldset> 5 <legend> 6 <?php echo __('Please enter your mailaddress and password'); ?> 7 </legend> 8 <?php echo $this->Form->input('mail_address'); 9 echo $this->Form->input('password'); 10 ?> 11 </fieldset> 12<?php echo $this->Form->end(__('Login')); ?> 13</div>
どのようなエラーメッセージが表示されるのか、表示されずに結果として判断しているのか、など、もう少しわかりやすく状況を説明してください。
app/tmp/error.logには特に何も出力されておらず、UsersController.phpのlogin()関数内の$this->Auth->login()の値を出力したところ、NULLの値となります。他にどのログや関数の出力値等の情報によるエラーメッセージが必要等あれば教えて頂けますでしょうか。
回答2件
あなたの回答
tips
プレビュー