2012-04-20 30 views
6

Theo quan điểm của tôi, tôi có:CakePHP 2.0 Xác thực sử dụng email thay vì tên

<?php 
echo $this->Form->create('User', array("controller" => "Users", "action" => "login", "method" => "post")); 
echo $this->Form->input('User.email', array("label" => false)); 
echo $this->Form->input('User.password', array("label" => false, 'class' => 'password-input')); 
echo $this->Form->end(); ?> 

Trong AppController tôi:

public $components = array(
     'Session', 
     'Auth' 
    ); 

    function beforeFilter(){ 
     $this->Auth->fields = array(
      'username' => 'email', 
      'password' => 'password' 
     ); 
    } 

Trong UsersController tôi:

function beforeFilter(){ 
     $this->Auth->allow('sign_up', 'login', 'logout', 'forgot_password'); 
     return parent::beforeFilter(); 
    } 
public function login() { 
     if ($this->Auth->login()) { 
      $this->Session->setFlash(__('Successfully logged in'), 'default', array('class' => 'success')); 
      $this->redirect($this->Auth->redirect()); 
     } else { 
      if (!empty($this->request->data)) { 
       $this->Session->setFlash(__('Username or password is incorrect'), 'default', array('class' => 'notice')); 
      } 
     } 
    } 

Nhưng tên đăng nhập là không làm việc, tôi đang thiếu gì?

Cảm ơn.

Trả lời

14

Tôi tin rằng vấn đề là:

function beforeFilter(){ 
    $this->Auth->fields = array(
     'username' => 'email', 
     'password' => 'password' 
    ); 
} 

Đó là cách các lĩnh vực đăng nhập tùy chỉnh được quy định tại CakePHP 1.3. CakePHP 2.0 thay vào đó yêu cầu bạn chỉ định các trường này trong số public $components = array(...);. 1.3 API cho thấy rằng Auth có thuộc tính $ fields, nhưng 2.0 API cho thấy rằng không còn thuộc tính $ fields nữa. Vì vậy, bạn phải:

thông tin
public $components = array(
    'Session', 
    'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'fields' => array('username' => 'email') 
      ) 
     ) 
    ) 
); 

có thể tìm thêm tại địa chỉ: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers

Xin vui lòng cho tôi làm thế nào nó hoạt động ra!

0

Giải pháp cuối cùng cho vấn đề của tôi. Cảm ơn bạn.

Tôi đã có một vấn đề với userModel và tôi viết những dòng này:

'Auth' => array(
     'userModel' => 'Member' 
    ) 

thay vì điều này:

'Auth' => array(
    'authenticate' => array(
     'Form' => array(
      'userModel' => 'Member' 
     ) 
    ) 
) 
Các vấn đề liên quan