How to create a admin login session in Codeigniter 4

How to create a admin login session in Codeigniter 4

Problem Description:

if($data)
{
  $pass = $data['password'];
  $authenticatePassword = password_verify($password, $pass);
  if($authenticatePassword)
  {
    $ses_data = [
      'id' => $data['id'],
      'name' => $data['name'],
      'email' => $data['email'],
      'isLoggedIn' => TRUE
    ];
    $session->set($ses_data);
    return redirect()->to('/profile');

I want to make it so the admin can log in and direct it to his page.

Solution – 1

do this in your admin controller

function index()
{
    if($data)
    {
        $username= $this->input->post('username');
        $password= $this->input->post('password');
        $row= $this->admins->adminLogin($username, $password);
        if(count($row)==1)
        {
            foreach($row as $val)
                {
                    $data= array('USERID' =>$val->id,
                            'USERNAME' => $val->username,
                            'logged_in' => true,
                            'id' => $val->id,
                            );
                        $this->session->set_userdata($data);
                }
            redirect('admin/dashboard');
        }
        else
        {
          $this->session->set_flashdata('message','<div class="alert alert-danger">Invalid Username and Password.</div>');
        }
    }
    $this->load->view('admin/index');
}
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject