I am using the github repository code here: https://github.com/hunzinker/CakePHP-Auth-Forgot-Password
I have used the following function in my UsersController.php
. I get the error Undefined index: token on the line that has a comment before it. What should I change?
/**
* Allow user to reset password if $token is valid.
* @return
*/
function reset_password_token($reset_password_token = null) {
if (empty($this->data)) {
$this->data = $this->User->findByResetPasswordToken($reset_password_token);
if (!empty($this->data['User']['reset_password_token']) &&
!empty($this->data['User']['token_created_at']) &&
$this->__validToken($this->data['User']['token_created_at'])
) {
$this->data['User']['id'] = null;
$_SESSION['token'] = $reset_password_token;
} else {
$this->Session->setflash(
'The password reset request has either expired or is invalid.'
);
$this->redirect('/users/login');
}
} else {
//ERROR ON THE NEXT LINE HERE UNDEFINED INDEX: TOKEN
if ($this->data['User']['reset_password_token'] != $_SESSION['token']) {
$this->Session->setflash(
'The password reset request has either expired or is invalid.'
);
$this->redirect('/users/login');
}
$user = $this->User->findByResetPasswordToken(
$this->data['User']['reset_password_token']
);
$this->User->id = $user['User']['id'];
if ($this->User->save($this->data, array('validate' => 'only'))) {
$this->data['User']['reset_password_token'] =
$this->data['User']['token_created_at'] = null;
if ($this->User->save($this->data) &&
$this->__sendPasswordChangedEmail($user['User']['id'])
) {
unset($_SESSION['token']);
$this->Session->setflash(
'Your password was changed successfully. Please login to continue.'
);
$this->redirect('/users/login');
}
}
}
}
You need to be sure that $_SESSION contains this index, so you should update it like this in order to be sure it exists:
By this: