• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mythauth - update password form

#10
(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! Smile

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! Smile
Thank you very much,very helpfull with the code that you provide.
Reply


Messages In This Thread
Mythauth - update password form - by schwaluck - 03-21-2021, 08:02 AM
RE: Mythauth - update password form - by InsiteFX - 03-21-2021, 08:49 PM
RE: Mythauth - update password form - by InsiteFX - 03-22-2021, 10:43 AM
RE: Mythauth - update password form - by ikesela - 03-22-2021, 10:37 PM
RE: Mythauth - update password form - by fuzna28 - 03-31-2021, 08:49 AM
RE: Mythauth - update password form - by fuzna28 - 04-02-2021, 06:43 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.