password_hash in codeIgniter4

<?php

namespace App\Controllers;
use App\Models\UserModel;
class Home extends BaseController
{
    public function index()
    {
        return view('welcome_message');
    }
    public function register(){

        $session =  \Config\Services::session();
        $session->set("isLogin","loginned");

        if($this->request->getMethod()=="get"){
            echo view("register");
        }
        else if($this->request->getMethod()=="post"){
            if($this->validate([
                'name'=>'required',
                'email'=>'required',
                'password'=>'required'
            ])){
                $model=new UserModel();
                $data = [
                'name'     => $this->request->getVar('name'),
                'email'    => $this->request->getVar('email'),
                'password' => password_hash($this->request->getVar('password'), PASSWORD_DEFAULT)
            ];
            $model->save($data);
            }
            else{
                return redirect()->back()->withInput();
            }
        }    	
    }
    public function login(){
    	if($this->request->getMethod()=="get"){
            echo view("login");
        }
        else if($this->request->getMethod()=="post"){
            $model=new UserModel();
            $data=$model->where("email", $this->request->getVar("email"))->first();
            if(!is_null($data)){
                if(password_verify($this->request->getVar("password"), $data['password'])){
                    echo "loginned";
                }
                else{
                    echo "invalid password";
                }
            }
            else{
                echo "No record matched";
            }
        }
    }   
}
© 2016 - 2022, All Rights are Reserved.