-
fuzna28
Newbie
-
Posts: 2
Threads: 0
Joined: Mar 2021
Reputation:
0
(04-01-2021, 01:22 AM)schwaluck Wrote: (03-31-2021, 08:49 AM)fuzna28 Wrote: (03-23-2021, 03:04 AM)schwaluck Wrote: (03-22-2021, 10:37 PM)ikesela Wrote: use given user entity class to get hash of new password.
Code: $entity = new User();
$newPassword = $this->request->getPost('new_password');
$entity->setPassword($newPassword);
$hash = $entity->password_hash;
$users->update($id,['password_hash' => $hash]);
Hey ikesela,
actually an obvious solution, but I didn't think of it. Thank you , it works like a charm now!
Have a nice day! Hello,can i see your full source for change password ? Im so frustased right now because dont understand,thank you. Sorry for my bad english language. Hey fuzna28,
I think the interesting part for you is the update function in the controller. So here is what i did:
Code: namespace App\Controllers;
use Config\Services;
use Myth\Auth\Models\UserModel;
use Myth\Auth\Entities\User;
class Test extends BaseController
{
public function updatePassword() {
//Rules for the update password form
$rules = [
'old-password' => [
'label' => 'old password',
'rules' => 'required|checkOldPasswords',
'errors' => [
'required' => 'Put your message here',
'checkOldPasswords' => 'Put your message here',
]
],
'new-password' => [
'label' => 'new password',
'rules' => 'required',
'errors' => [
'required' => 'Put your message here',
]
],
'confirm-new-password' => [
'label' => 'confirm password',
'rules' => 'required|matches[new-password]',
'errors' => [
'required' => 'Put your message here',
'matches' => 'Put your message here'
]
],
];
if ($this->request->getMethod() === 'post' && $this->validate($rules)) {
//Create new instance of the MythAuth UserModel
$users = model(UserModel::class);
//Get the id of the current user
$user_id = user_id();
//Create new user entity
$entity = new User();
//Get current password from input field
$newPassword = $this->request->getPost('new-password');
//Hash password using the "setPassword" function of the User entity
$entity->setPassword($newPassword);
//Save the hashed password in the variable "hash"
$hash = $entity->password_hash;
//update the current users password_hash in the database with the new hashed password.
$users->update($user_id,['password_hash' => $hash]);
//Return back with success message
return redirect()->to('/test')->with('success', "Put your message here");
}
else {
//Return with errors
return redirect()->to('/test')->withInput()->with('error', "Put your message here");
}
}
}
I hope that helps. If you have any questions, just ask! Thank you very much,very helpfull with the code that you provide.
|