Reputation:
I am trying to use Auth in CakePhp. I followed some other post but none of them seem to work. Here's what I have:
AppController.php
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'BlowFish',
'fields' => array('email' => 'email', 'password' => 'password')
)
),
'loginRedirect' => array('controller' => 'poll', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'public', 'action' => 'index'),
'flash' => array(
'element' => 'alert',
'key' => 'auth',
'params' => array(
'plugin' => 'BoostCake',
'class' => 'alert-error'
)
)
)
);
User.php (model)
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class User extends AppModel{
public function beforeSave($option = array()){
$passwordHasher = new BlowfishPasswordHasher();
$this->data['User']['password'] = $passwordHasher->hash($this->data['User']['password']);
return true;
}
}
UserController.php
public function login(){
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash('Incorrect email and password');
}
}
}
Login.php
<h1>Login</h1>
<?php
echo $this->Form->create();
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->end('Authenticate')
?>
Upvotes: 0
Views: 471
Reputation:
Found the error. Even though it the input field on the form is "email", in Auth, it is consider "username"
so
echo $this->Form->input('email');
echo $this->Form->input('password');
translates to this
fields' => array('username' => 'email', 'password' => 'password')
Upvotes: 1