login with email or username codeigniter 4

To implement a login functionality in CodeIgniter 4 that allows users to log in using either their email or username, you can follow these steps:

  1. Create a Database Table: First, create a database table to store user information, including their email, username, and password. You can use CodeIgniter's migration feature to create the table.

  2. Create a Model: Create a model that will handle database interactions for the login process. This model will be responsible for retrieving user information based on the provided email or username.

  3. Create a Controller: Create a controller that will handle the login functionality. This controller will load the login view, validate the user's input, and authenticate the user.

  4. Create a View: Create a view file that will contain the login form. This form should have fields for both email and username, along with a field for the password.

  5. Handle Form Submission: In the controller, handle the form submission by retrieving the user's input and validating it. Use the model to check if a user with the provided email or username exists in the database.

  6. Authenticate User: If the user exists, authenticate their password. You can use CodeIgniter's password hashing functionality to securely compare the provided password with the stored hashed password.

  7. Set Session: If the user's credentials are valid, set a session to keep the user logged in. CodeIgniter provides a session library that you can use for this purpose.

  8. Redirect: After successful authentication and session creation, redirect the user to the desired page.

Here is a sample code snippet that demonstrates the implementation of the login functionality with email or username in CodeIgniter 4:

// UserController.php

namespace App\Controllers;

use App\Models\UserModel;
use CodeIgniter\Controller;

class UserController extends Controller
    public function login()
        // Load the login view
        return view('login');

    public function authenticate()
        // Retrieve user input
        $emailOrUsername = $this->request->getPost('email_or_username');
        $password = $this->request->getPost('password');

        // Instantiate the User model
        $userModel = new UserModel();

        // Check if a user with the provided email or username exists
        $user = $userModel->getByEmailOrUsername($emailOrUsername);

        if ($user) {
            // Verify the password
            if (password_verify($password, $user['password'])) {
                // Set session for the authenticated user
                $session = session();
                $session->set('user_id', $user['id']);

                // Redirect to the desired page
                return redirect()->to('/dashboard');

        // Authentication failed, redirect back to the login page
        return redirect()->to('/login')->with('error', 'Invalid credentials');

In this example, the login method is responsible for loading the login view, while the authenticate method handles the form submission and authentication process. The UserModel is used to retrieve user information based on the provided email or username.

This code assumes that you have already set up the necessary database configuration and autoloaded the necessary libraries and helpers in your CodeIgniter 4 application.