Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to change user password in CakePhp using Auth

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 4.67k
    Comment on it

    Hi All,
    In this blog we will discuss how to change the password in cakePhp by using Auth.

    Firstly, in your model you have to define a function beforeSave which will hash your password to auth password, you can do this by writing the below code:

    public function beforeSave($options = array()) {
    
            if (isset($this->data[$this->alias]['password'])) {
    
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
            }
            return true;
        }
    

    In your change Password function you have to write-

        public function changePassword() {
    
            if ($this->data) {
                if (!empty($this->data['User']['password']) && !empty($this->data['User']['repassword'])) {
                    if ($this->data['User']['password'] == $this->data['User']['repassword']) {
                    $userInfo=$this->User->findById($this->data);
                      if(AuthComponent::password($this->data['User']['old_password'])==$userInfo['User']['password']){                  
    if ($this->User->save($this->data)) {
                            $this->Session->setFlash('Password Changed Successfully', 'default', array('class' => 'green'));
                            $this->redirect('/admin/Users/dashboard');
    
                        } }else {
                            $this->Session->setFlash('Unable to save password', 'default', array('class' => 'red'));
                            $this->redirect('/admin/Users/changePassword');
                        }
                    } else {
                        $this->Session->setFlash('Password and Re password does not match', 'default', array('class' => 'red'));
                        $this->redirect('/admin/Users/changePassword');
                    }
                } else {
                    $this->Session->setFlash('Password and Re password cannot be empty', 'default', array('class' => 'red'));
                    $this->redirect('/admin/Users/changePassword');   
                }
            }
        }
    

    Lastly, In your view.ctp file you have write the below code:

    echo $this->Form->create('User');
    echo $this->Form->input('id');
    echo $this->Form->input('old_password');
    echo $this->Form->input('password1');
    echo $this->Form->input('repassword');
    echo $this->Form->end('Submit');
    

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: