简体   繁体   中英

CakePHP Changing Auth Fields isn't working

I'm currently trying to change my cakephp login from using the username field to using the email field.

When using the username field, the login works fine, here's the code for the login with username:

login.ctp

<?php
echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end(__('Login'));
?>

UsersController.php

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'login'); // Letting users register themselves
    $this->Auth->fields = array('username' => 'username', 'password' => 'password');
}
public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->Session->setFlash(__('worked'));
        } else {
            debug($this->data);
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

AppController

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

So this all works fine, I can login with test details.

So to change this to using an email, all I have done is:

  • Change the input in login.ctp from username to email

     echo $this->Form->input('email'); 
  • Change the fields array in userscontroller from username to email

     $this->Auth->fields = array('username' => 'email', 'password' => 'password'); 
  • Change the database field from username to email

I then try to login using the same test details and it tells me they are incorrect.

Does anyone have any idea why this wouldn't work?

In Cake 2.x it's a little different

$this->Auth->authenticate = array(
    'Form' => array(
        'fields' => array('username' => 'email', 'password' => 'password'),
    ),
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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