I followed tutorial on CakephpTV to make registration by AuthComponent. Yesterday everything worked good, but today it isn't.
When I'm trying to add new account it says that passwords do not match, and in password field there is hashed password and in password confirmation there is normal (not hashed) password.
My UserController class:
class UsersController extends AppController
{
function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('new_user');
if($this->action == 'add' || $this->action == 'edit') {
$this->Auth->authenticate = $this->User;
}
}
function new_user()
{
if (!empty($this->data)) {
if ($this->User开发者_JS百科->save($this->data)) {
$this->Session->setFlash('Rejestracja zakończona pomyślnie');
$this->redirect(array('action' => 'index'));
}
}
}
function login() {
}
function logout() {
$this->redirect($this->Auth->logout());
}
}
My User model:
class User extends AppModel {
var $name = 'User';
var $validate = array (
'email' => array(
'Please supply a valid email address.' => array(
'rule' => 'email',
'message' => 'Please supply a valid email address.'
),
'Already exists' => array(
'rule' => 'isUnique',
'message' => 'Must be unique'
)
),
'password' => array (
'aThis field must have between 6 and 40 alphanumeric characters.' => array(
'rule' => '/^[^\'"]{6,40}$/i',
'message' => 'aThis field must have between 6 and 40 alphanumeric characters.'
),
'Do not match' => array(
'rule' => 'matchPasswords',
'message' => 'Do not match'
)
)
);
function matchPasswords($data) {
if($data['password'] == $this->data['User']['password_confirmation']) {
return TRUE;
}
$this->invalidate('password_confirmation', 'DO not match');
return FALSE;
}
function hashPasswords($data) {
if(isset($this->data['User']['password'])) {
$this->data['User']['password'] = Security::hash($this->data['User']['password'], NULL, TRUE);
return $data;
}
return $data;
}
function beforeSave() {
$this->hashPasswords(NULL, TRUE);
$this->data['User']['registration_date'] = date("Y-m-d H:i:s");
return TRUE;
}
}
My new_user.ctp:
echo $this->Form->create('User', array('action' => 'new_user'));
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('password_confirmation',array('type' => 'password'));
echo $this->Form->end('Zarejestruj');
My AppController class:
class AppController extends Controller {
var $components = array('Auth','Session');
function beforeFilter() {
$this->Auth->fields = array('username' => 'email', 'password' => 'password');
$this->Auth->allow('index','view','display','new_user');
$this->Auth->authError = 'Zaloguj się aby zobaczyć tą stronę.';
$this->Auth->loginError = 'Wprowadzono niepoprawne dane.';
$this->Auth->loginRedirect = array('controller'=>'pages','action'=>'home');
$this->Auth->loginRedirect = array('controller'=>'pages','action'=>'home');
}
}
CakePHP automatically hashes fields named password
in a User
model. This is why you get an already hashed password back when the form fails. You might want to check if the hash you get back is the same hash as you'd expect to get back.
Apart from that, in your function matchPasswords
your checking on $this->data['password']
whereas I think that should be $this->data['User']['password']
.
精彩评论