Codeigniter 4 Authentication Login and Registration

The most common way of authenticating a user in web applications is through a login form. It is usually used to check the authenticity of the use. In this tutorial, we will create a Codeigniter 4 Authentication Login and Registration with MySQL. So here we have handled login functionality by creating a simple login form in CodeIgniter and Bootstrap Framework along with MySQL Database. This is a very simple example, you can just copy-paste and change according to your requirement.
Before started to implement the Codeigniter 4 Authentication Login and Registration, look files structure:
  • codeigniter-authentication-login-registration
    • app
      • Config
        • App.php
        • Constants.php
        • Database.php
        • Routes.php
        • Filters.php
      • Filters
        • AuthGuard.php
      • Controllers
        • User.php
      • Models
        • UserModel.php
      • Views
        • user
          • register.php
          • login.php
          • dashboard.php
        • templates
          • header.php
          • footer.php
          • menus.php
    • public
      • .htaccess
      • index.php
    • .htaccess
    • .env
    • assets
      • images
      • css
        • style.css
        • sticky-footer-navbar.css
Step 1 – Install Codeigniter 4 Application
To handle the actual install you would use the following command in your terminal or visit Codeigniter site and download the Codeigniter application.
Display Errors
You may turn on the feature to errors, go to the app/Config/Boot/production.php and change display_errors prop value to 1 from 0.
Step 2 – Basic App Configurations
Now, you need to some basic configuration on the app/config/app.php file.
Step 3: Create the Database and Table
For this tutorial, you need a MySQL database with the following table:
Step 4: Setup and Configure Database access
Update the file app/Config/Database.php OR .env file:
i) .env
ii) Database.php
Step 5: Create Filter file
Add code the file app/Filters/AuthGuard.php
Step 6: Update Filter file
Add code the file app/Config/Filters.php
Step 7: Update routes file
Add/Update code the file app/Config/Routes.php in your CodeIgniter installation with you controller’s name.
Step 8: Create and Update User Model
Create a model file named UserModel.php inside “app/Models” folder.
Step 9: Create controllers
Create a controllers file named User.php inside “app/Controllers” folder.
Step 10: Create views
Create a views file named register.php inside “add/Views/user” folder.
Step 11: Create views
Create a views file named login.php inside “add/Views/user” folder.
Step 12: Create views
Create a views file named dashboard.php inside “add/Views/user” folder.
Create files named (header.php, menus.php and footer.php)
This file contains the header and footer section of the webpage. The Bootstrap library is used to provide a better UI, so, include it in the header and footer section.
i) header.php
ii) menus.php
iii) footer.php