Hello Readers ,
Their are many methods to hash password in cakephp like cakephp alias , md5 etc blowfish is also one of them . One thing we have to note down that we can’t use Blowfish if we already have a database filled with passwords hashed using another method.
In the AppController write this code .
<?php
class AppController {
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
);
}
Use before save method to hash the password . In the User Model.
<?php
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
public function beforeSave($options = array()) {
// if ID is not set, we're inserting a new user as opposed to updating
if (!$this->id) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']);
}
return true;
}
}
In the User Controller .
<?php
class UsersController extends AppController {
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash( __('Username or password incorrect'));
}
}
}
}
And that’s all there is to it.
0 Comment(s)