Steven Marks
Steven Marks

Reputation: 704

CakePHP 2.4 Forgotten Password

I have just started using CakePHP and love using it! I have created a login system and registration system, however am really struggling with the "forgotten password" section.

I want to use a tokenhash and expiry date in the Users DB so that it cant be abused, users would need to enter username and email to get an activation link emailed to them with a newly generated tokenhash

There are quite a few tutorials out there but I find most of them work for the first part e.g. emailing the activation link/ resetting token and timer but all seem to fail on the change of the password.

Please help me, either with a working tutorial from the net or a solution that applies the above required things.

Thanks in advance Steve

Upvotes: 0

Views: 277

Answers (1)

Dinesh Rawat
Dinesh Rawat

Reputation: 1019

Below I am writing the code that I wrote for one of my project, this might help you out.

1- I created a new table which contains the unique token for every user.

Table Name:- user_password_resets

Columns : userclient_id, token

2- A email template name as:- change_password.html inside /webroot/template/change_password.html

public function login_send() {
       $this->isLoggedIn(); //Check if the user is logged in
      if($this->request->is('post')) { #if the form is submitted
        $login = $this->data['User']['login'];
        $conditions = array('User.login'=>$login);
        if($this->User->hasAny($conditions)) {
            $users = $this->User->find('first', array('conditions'=>$conditions));
            #Generate the token
            $token = md5(uniqid(rand(),true));
            #Save token and other details in user_password_reset_links table
            $users = $this->User->find('first', array('conditions'=>array('User.login'=>$login)));
            $my_name = $users['User']['first_name'];
            $reset_links = array();
            $reset_links['UserPasswordReset']['userclient_id'] = $users['User']['client_id'];
            $reset_links['UserPasswordReset']['token'] = $token;

            $conditions = array('UserPasswordReset.userclient_id'=>$users['User']['client_id']);
            if($this->UserPasswordReset->hasAny($conditions)) {
                 $user_id = $users['User']['client_id'];
                $this->UserPasswordReset->updateAll(array('UserPasswordReset.token'=>"'$token'"), array("UserPasswordReset.userclient_id"=>"$user_id"));    
            } else {
                $this->UserPasswordReset->create();
               $this->UserPasswordReset->save($reset_links);
            }
            $password_reset_link = BASE_URL."users/reset_password/$token";

            #Send Welcome Email
            $mailContent = file_get_contents(BASE_URL . "templates/change_password.html");
            $rootlink = BASE_URL;
            $arrMail = array(
                "{NICK}" => ucfirst($my_name),
                "{rootlink}" => BASE_URL,
                "{SITE_TITLE}" => SITE_TITLE,
                "{PASSWORD_RESET_LINK}"=>$password_reset_link
                );

             $mails = explode(',', $users['User']['email']);    
            $msg = @str_replace(array_keys($arrMail), array_values($arrMail), $mailContent);
            $data = array();
            $data['to'] = @$mails[0];
            $data['body'] = $msg;
            $data['subject'] = SITE_TITLE.'- Reset Password.';
            $this->send_mail($data);

            $this->Session->setFlash('A password reset link has been sent to the email address.', 'default', array('class'=>'successMsg'));
            $this->redirect(array('controller'=>'users', 'action'=>'login'));
            exit;
        } else {
            $this->Session->setFlash('The Username entered is not registered with Captain Marketing.', 'default', array('class'=>'errorMsg'));
            $this->redirect(array('controller'=>'users', 'action'=>'login_send'));
            exit;
        }
    }
    $this->set('title_for_layout', '-Send password reset link');
  }

Upvotes: 2

Related Questions