cakephp错误:用户名和密码无效,请重试

时间:2014-11-28 07:00:18

标签: cakephp

我坚持认为我的cakephp登录无效。我正在同一个视图上创建登录和注册。期待帮助。我的代码也是。

     public function login(){
         (isset($this->request->data['User'])) { // Check if the login Form was submitted
                  if (!empty($this->request->data['User']) && $this->Auth->login()) {

                 return $this->redirect($this->Auth->redirect());
                    }
                    $this->request->data['User']['password'] = '';
    $this->Session->setFlash(__('Invalid username or password, try again'));
}

}

此功能的模型是

    class User extends AppModel{
     public $validate = array(
     'email'=>'email',
    'password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A password is required'
        )
    ),);

视图是:

      <div><?php echo $this->Session->flash(); ?></div>
      <?php echo $this->Form->create("User");  ?>
     <?php echo $this->Form->input('email' ,array('label'=>"Email :")); ?>
     <?php echo $this->Form->input('password',array('label'=>"Password"));?>
     <?php echo $this->Form->end('Login'); ?>

使用注册在数据库中正确保存哈希密码,但是当我在登录时输入密码时,它会提供无效的用户名和密码错误。 我的AppController是

 class AppController extends Controller {
         public $components = array(
       'Auth'=> array(
        'authenticate' => array(
        'Form' => array(
            'fields' => array('username' =>'email','password'=>'password')
        )
    )
),
    'Session',
    'Auth' => array(
        'loginRedirect' => array(
            'controller' => 'users',
            'action' => 'profileindex'
        ),
        //'authorize' => array('Controller'),
                'loginAction' => array('controller' => 'users', 'action' => 'login'),
                'loginRedirect' => array('controller' => 'users', 'action' => 'profileindex'),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
                'authError' => 'You don\'t have access here.',

        'logoutRedirect' => array(
            'controller' => 'users',
            'action' => 'login',
            'home'
        ),
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish'
            )
        )
    )
);

1 个答案:

答案 0 :(得分:0)

<?php
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

    public function beforeSave($options = array()) {


            $passwordHasher = new BlowfishPasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']);

        return true;
    }
}