简体   繁体   English

在CakePHP Auth Component中使用电子邮件而不是用户名

[英]Using email instead of username in CakePHP Auth Component

I am working on cakephp 2.x . 我正在开发cakephp 2.x. my problem is i dont want to use username for logging .. i am taking the email and password from the user and verify this email and password from the database i have a table in my database name user and it has 3 fields id , email and password 我的问题是我不想使用用户名进行记录..我正在从用户那里获取电子邮件和密码,并从数据库验证这个电子邮件和密码我在我的数据库名称用户中有一个表,它有3个字段ID电子邮件密码

here is my code 这是我的代码

Model 模型

 <?php
class User extends AppModel {
public $useTable = 'user';
}
?>

AppController AppController的

 class AppController extends Controller {
   public $components = array(
   'Session',
'Auth'=>array(
    'loginRedirect'=>array('controller'=>'users', 'action'=>'admin'),
    'logoutRedirect'=>array('controller'=>'users', 'action'=>'admin'),
    'authError'=>"You can't access that page",
    'authorize'=>array('Controller') 
   )
);

public function isAuthorized($user) {
}

  public function beforeFilter() {
  $this->Auth->allow('index');

UserController UserController的

public function login()
   {
     if ($this->request->is('post')) {
       if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash('Your email/password combination was incorrect');
    }
}
}

login.ctp login.ctp

 <?php


   echo $this->form->create();

  echo $this->form->input('email');
  echo $this->form->input('password');


    echo $this->form->end('Authenticate');
   ?>

You can configure it with: 您可以配置它:

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

See also Configuring Authentication handlers in the cookbook. 另请参阅cookbook中的配置身份验证处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM