In this article, we will discuss, How to create a Login with MySQL database using the CodeIgniter 4.

1. Copy and Paste the following lines in app/Config/Routes.php

$routes->get('/', 'Home::index', ['filter' => 'auth']);
$routes->add('login', 'Signin::login');
$routes->post('signin', 'Signin::signin');

2. Create a signin.php file in the app/Views/ directory

<p class="login-box-msg">Sign in to start your session</p>
<form action="<?php echo base_url('signin')?>" method="post" autocomplete="off">
    <div class="form-group has-feedback">
        <input type="email" name="email" id="name" class="form-control" placeholder="Email"> 
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
    </div>
    <div class="form-group has-feedback">
        <input type="password" id="password" name="password" class="form-control" placeholder="Password"> 
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
    </div>
    <div class="row">
        <div class="col-xs-8">
        </div>
        <div class="col-xs-4">
            <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
        </div>
    </div>
</form>

3. Create a model file SigninModel.php in the app/Models/ directory.

<?php
namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\DatabaseConnectionInterface;

class SigninModel extends Model {

    protected $db;
    public function __construct(ConnectionInterface &$db) {
        $this->db =& $db;
    }

    public function clientLogin($email, $password) {
        $result = $this->db
                        ->table('user_info')
                        ->where(["status" => "1", "email" => $email, "password" => $password])
                        ->get()
                        ->getRow();
        if($result) {
            $data = [
                'login_id'          => $result->id,
                'login_name'        => $result->name,
                'login_email'       => $result->email,
                'login_status'      => TRUE,
            ];
            return $data;
        } else {
            return 0;
        }
        
    }
}

4. Create a controller file Signin.php in the app/Controllers/ directory.

<?php
namespace App\Controllers;

use App\Models\SigninModel;

class Signin extends BaseController {
    
    public function __construct() {
        $db = db_connect();
        helper(['url', 'form', 'array']);
        $this->session   = ConfigServices::session();
        $this->loginModel = new SigninModel($db);

    }
    
    public function index() {
        $this->login();
    }
    
    public function login() {
        echo view("signin");
    }

    public function signin() {
        $email      = $this->request->getPost('email');
        $password   = $this->request->getPost('password');

        $result     = $this->loginModel->clientLogin($email, md5(md5($password)));
        if($result) {
            $this->session->set($result);
            return redirect()->route('home/');
        } else {
            return redirect()->route('login');
        }
    }
    
}

5. Create a controller file Home.php in the app/Controllers/ directory.

<?php
namespace App\Controllers;

class Home extends BaseController {

    public function __construct() {
        helper(['url', 'form', 'array']);
        $this->session   = ConfigServices::session();
    }
    
    public function index() {   
        echo "Welcome";
    }

}

6. Create a controller file AuthFilter.php in the app/Filters/ directory.

<?php
namespace AppFilters;

use CodeIgniterHTTPRequestInterface;
use CodeIgniterHTTPResponseInterface;
use CodeIgniterFiltersFilterInterface;

class AuthFilter implements FilterInterface {

    public function before(RequestInterface $request, $arguments = null) {
        if(! session()->get('login_status')){
          return redirect()->route('login');
        }

    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) {
        // Do something here
    }
}