Using email instead of username in CakePHP Auth

I am working on cakephp 2.x. my problem is that I don't want to use the username for logging. I take the email address and password from the user and check this email and password from the database I have a table in the name of my database and it has 3 fields id , email and password

here is my code

Model

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

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

 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

  <?php echo $this->form->create(); echo $this->form->input('email'); echo $this->form->input('password'); echo $this->form->end('Authenticate'); ?> 
+4
source share
1 answer

You can configure it with:

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

See also Configuring authentication handlers in a cookbook.

+8
source

Source: https://habr.com/ru/post/1486345/


All Articles