Cannot Get Auth Authentication for CakePHP 2.0

I am trying to get a simple login form to work using CakePHP 2.0 ... just Auth, now there is no ACL.

I can see the form and enter the email address and password as they are in the database, but I just go back to the form and an error message is displayed. Here is my code:

AppController:

class AppController extends Controller { function beforeFilter() { $this->Auth->userModel = 'Users'; $this->Auth->fields = array('username' => 'email', 'password' => 'password'); //have to put both, even if we're just changing one $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login'); $this->Auth->loginRedirect = array('controller' => 'hotels', 'action' => 'dashboard'); $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login'); } } 

login.ctp:

 <?php echo $this->Form->create('User', array('action' => 'login')); echo $this->Form->input('email'); echo $this->Form->input('password'); echo $this->Form->end('Login'); ?> 

UsersController:

  class UsersController extends AppController { var $name = 'Users'; var $helpers = array('Html','Form'); var $components = array('Auth','Session'); function beforeFilter() { $this->Auth->allow("logout"); parent::beforeFilter(); } function index() { } //Redirects to login() function login() { if ($this->Auth->login()) { $this->redirect($this->Auth->redirect()); } else { $this->Session->setFlash(__('Invalid username or password, try again')); } } function logout() { $this->redirect($this->Auth->logout()); } } ?> 

I appreciate any help with this. Thanks!

+7
source share
7 answers

The error "Invalid username or password, try again" appears after you clicked on the username?

There are a few things you should check out:

• Is the output of $this->Auth->login() identical to the information in your database? Place debug($this->Auth->login()) to see the result in your login method after submitting the form.

• Are passwords in the database correctly hashed?

• Try making AuthComponent available to all of your controllers, not just UsersController .

• Not sure if this matters, but call parent::beforeFilter(); first of all in your beforeFilter control beforeFilter .

EDIT:

Sees what you are trying to verify based on email and password. By default, AuthComponent expects a username and password. You must explicitly indicate that you want the email address and password to be verified with $this->Auth->login() . This comes from the 2.0 documentation:

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

I suppose to expect that you do not see SQL output.

+9
source

You should also check if your password is set to VARCHAR 50 in the database.

It seems to me that I truncated the hashed password in DB, and Auth never happened.

+6
source

if you do not use defalut "username", "password" for authorization, you cannot log in, for example, you use "email"

you should edit the component declaration in the controller containing your login function:

 $component = array('Auth' => array( 'authenticate' => array( 'Form' => array( 'fields' => array('username' => 'email', 'password' => 'mot_de_passe') ) ) )); 
+2
source

Be careful with cakephp conventions. You have to change this "$ this-> Auth-> userModel =" Users "; to" $ this-> Auth-> userModel = ' User '; "because the user without the plural is the model convention in the cake. This worked for me as well as capital letters ... it almost drove me crazy.

+1
source
 public $components = array( 'Session', 'Auth' => array( 'loginRedirect' => array( 'controller' => 'Events', 'action' => 'index' ), 'logoutRedirect' => array( 'controller' => 'Users', 'action' => 'login', 'home' ), 'authenticate' => array( 'Form' => array( 'fields' => array('username' => 'username','password' => 'password') ) ) ) ); 
0
source

Editing a component declaration in AppController did the trick for me. If you have fields with names other than "username" and "password", you should always specify them. In your case it will be

 public $components = array( 'Auth' => array( 'authenticate' => array( 'Form' => array( 'passwordHasher' => 'Blowfish', 'fields' => array('username' => 'email','password' => 'password') ) ) ) ); 
0
source

There is an error in the cakephp tutorial. $ this-> Auth-> login () should be changed to $ This-> Auth-> Login ($ this-> request-> data)

-one
source

All Articles