Q:

How to encode password in cakephp using default security methods?

I am using cakephp2. I wants to encode my password before it save to the database. These codes are used in my model as,
App::uses('AppModel', 'Model');

class User extends AppModel {
function registration($name,$uname,$pass){
    $newpass = AuthComponent::password($pass);
    $reg=array(
            'name'=>$name,
            'uname'=>$uname,
            'password'=>$newpass,
            'status'=>'1'
        );
    $this->save($reg);
}

}
?>
But while inserting I am getting an error message as,
"Error: Class 'AuthComponent' not found
File: C:\xampp\htdocs\blog\app\Model\User.php".
How to solve this error ?

A:

First step is to make sure you have added Auth component, you can do this in appController.php
public $components = array(
                 'Auth'
          );

Then use your code   
App::uses('AppModel', 'Model');

class User extends AppModel {
function registration($name,$uname,$pass){
    $newpass = AuthComponent::password($pass);
    $reg=array(
            'name'=>$name,
            'uname'=>$uname,
            'password'=>$newpass,
            'status'=>'1'
        );
    $this->save($reg);
}

i do not think its a good practice to make insertion queries in model. instead a better way is to use the beforesave method in user.php(model) and then make insertion in the controller.

