Hello, I'm trying to make a "forgot my password" for the user to change the password in case he forgets and I'm having problems because he is entering the bank but at the time of login he says that the password is wrong. Follow the snippet of my code.
public function esqueci_senha() {
if ($this->request->is(['post'])) {
$myemail = $this->request->getData('email');
$mytoken = Security::hash(Security::randomBytes(25));
$userTable = TableRegistry::get('Usuarios');
$usuario = $userTable
->find('all')
->where(['email'=>$myemail])
->first();
$usuario->token = $mytoken;
$userTable->save($usuario);
Email::configTransport('recuperar_senha', [
'className' => 'Smtp',
'host' => MAIL_HOST,
'port' => MAIL_PORT,
'timeout' => 30,
'username' => MAIL_USER,
'password' => MAIL_PASS,
'client' => null,
'tls' => false,
]);
$email = new Email();
$email
->transport('recuperar_senha')
->template('recuperar_senha')
->emailFormat('html')
->from([MAIL_USER => 'TrucaoWeb'])
->to($myemail)
->subject('Por favor confirme alteração de sua senha')
->viewVars([
'email' => $this->request->data['email'],
'usuario' => $usuario['name'],
'token'=>$mytoken
]);
if ($email->send()) {
$this->Flash->success(__('Cheque seu email para alterar a senha.'));
return $this->redirect('/login');
} else {
$this->Flash->error(__('Não foi possível enviar o email: ') . $email->smtpError);
}
}
}
public function recuperar_senha($token) {
if($this->request->is(['post'])){
$userTable = TableRegistry::get('Usuarios');
$usuario = $userTable
->find('all')
->where(['token'=>$token])
->first();
$hasher = new DefaultPasswordHasher();
$usuario->password = $hasher->hash($this->request->data['password']);
// if($this->request->data['Nova_senha'] != $this->request->data['Repitir_nova_senha']) {
// $this->Flash->error('Senha nova não corresponde');
if($userTable->save($usuario)){
$this->Flash->success(__('Senha alterada com sucesso!'));
return $this->redirect('/login');
}else{
$this->Flash->error(__('Não foi possivel alterar sua senha, por favor tente novamente!'));
}
}
}