Hi,
In this tutorial we can learn that how to make reset password functionality in CakePHP . Most of the times users forget their password for their login credentials, so reset password functionality gives them the authority to change their user account password.
here is the function of reset password that you have to paste in your controller
// Forgot password function
function forgetpwd(){
$this->User->recursive=-1;
if(!empty($this->data))
{
if(empty($this->data['User']['email']))
{
$this->Session->setFlash('Please Provide Your Email Adress that You used to Register with Us');
}
else
{
$email=$this->data['User']['email'];
$fu=$this->User->find('first',array('conditions'=>array('User.email'=>$email)));
if(!empty($fu))
{
$key = uniqid();
$hash = md5($key);
$url = Router::url( array('controller'=>'drivers','action'=>'reset'), true ).'/'.$key.'#'.$hash;
$ms=$url;
$ms=wordwrap($ms,1000);
$fu['User']['tokenhash']=$key;
$this->User->id=$fu['User']['id'];
if($this->User->saveField('tokenhash',$fu['User']['tokenhash'])){
//============Email================//
$Email = new CakeEmail();
$Email->config('smtp')
->template ('resetpw')
->from ('ishan.bhatnagar@evontech.com')
->to ($fu['User']['email'])
->subject ('Reset Your Example.com Password')
->emailFormat ('html')
->viewVars (array('ms' => $ms));
if ($Email->send()) {
$this->Session->setFlash(__('Check Your Email To Reset your password', true));
//============EndEmail=============//
}
else{
$this->Session->setFlash("Error Generating Reset link");
}
}
}
else
{
$this->Session->setFlash('Email does Not Exist');
}
}
}
}
// Reset function
function reset($token=null)
{
$this->User->recursive=-1;
if(!empty($token)){
$u=$this->User->findBytokenhash($token);
if($u){
$this->User->id=$u['User']['id'];
if(!empty($this->data)){
$this->User->data=$this->data;
$this->User->data['User']['email']=$u['User']['email'];
$new_hash=sha1($u['User']['email'].rand(0,100));//created token
$this->User->data['User']['tokenhash']=$new_hash;
$this->User->data['User']['password'] = AuthComponent::password($this->request->data["User"]["password"]);
if($this->User->validates(array('fieldList'=>array('password','password_confirm')))){
if($this->User->save($this->User->data))
{
$this->Session->setFlash('Password Has been Updated');
$this->redirect(array('controller'=>'drivers','action'=>'login'));
}
}
else{
$this->set('errors',$this->User->invalidFields());
}
}
}
else
{
$this->Session->setFlash('Token Corrupted,,Please Retry.the reset link work only for once.');
}
}
else{
$this->redirect('/');
}
}
just after copying the function you can make a new reset.ctp and forgetpwd.ctp file under view->users folder or any of the folder you are using for your project.
Under reset.ctp file you can paste the following code.This code is used to create the form and to call the function that is created in the controller
<?php
if(isset($errors)){
echo '<div class="error">';
echo "<ul>";
foreach($errors as $error){
echo"<li><div class='error-message'>$error</div></li>";
}
echo"</ul>";
echo'</div>';
}
?>
<h1 class="text-center">Enter your credentials below to change password</h1>
<?php echo $this->Form->create('User'); ?>
<ul class="login">
<?php echo $this->Session->flash(); ?>
<li>
<?php echo $this->Form->input('password',array("type"=>"password","name"=>"data[User][password]", 'id' => 'password')); ?>
</li>
<li>
<?php echo $this->Form->input('password_confirm',array("type"=>"password", 'id' => 'confirm_password', "name"=>"data[User][password_confirm]")); ?>
</li>
<li>
<?php echo $this->Form->submit('CHANGE', array('class' => 'btnNew', 'type' => 'submit', 'div'=> false, 'value' => 'Save')); ?>
<label style="display: block;" for="password_confirm" class="error" id="message"></label>
<p>Click here to <a href="javascript:;">Sign up</a> now</p>
</li>
</ul>
<?php echo $this->Form->end(); ?>
Under forgetpwd.ctp you can add the following code to call function and to add the form
<div class="container">
<div class="midBlock clearfix">
<div class="col-md-12">
<h1 class="text-center">Enter your email below to <p>Reset your password</p></h1>
<?php echo $this->Form->create('User', array('action' => 'forgetpwd')); ?>
<ul class="login">
<?php echo $this->Session->flash(); ?>
<li>
<?php echo $this->Form->input('email', array('type' => 'text', 'placeholder' => 'Enter Email', 'label' => false)); ?>
</li>
<li>
<?php echo $this->Form->submit('Submit', array('class' => 'btnNew', 'type' => 'submit', 'div'=> false)); ?>
<p>Click here to <a href="javascript:;">Sign up</a> now</p>
</li>
</ul>
<?php echo $this->Form->end(); ?>
</div>
</div>
</div>
This reset password will send the mail to the user by which user can click on the link sent to his mail and can change the password
0 Comment(s)