0

so i have this function on my controllers to check if the user is logged in or not, but it wont redirect to the login page when user is not logged in

public function __construct()
    {
        if(session()->get('logged_in') !== true)
        {
            redirect('login');
        }
    }

anyone has a solution for this? or should i put the session check on every function? like

public function examplefunction()
{
   if(session()->get('logged_in') !== true)
   {
      redirect('login');
   }
   //run other codes
}
Tunku Salim
  • 167
  • 1
  • 9
  • 1
    Could you please check this - https://codeigniter4.github.io/userguide/incoming/filters.html – Dmitry Leiko Aug 25 '20 at 14:01
  • If you need to use a constructor in your class make sure you extend the parent constructor: example here: https://codeigniter.com/user_guide/extending/core_classes.html#extending-core-classes – Vickel Aug 25 '20 at 14:14
  • 1
    Does this answer your question? [Codeigniter redirect()->to() doesnt work in \_\_construct()](https://stackoverflow.com/questions/63048335/codeigniter-redirect-to-doesnt-work-in-construct) – ViLar Aug 25 '20 at 14:44

1 Answers1

5

in codeigniter 4 there is a class name filter u can create class for authentication, u can read the documentation for more details.

this is the solution for your problem

  1. create file Auth.php inside App/Filters folder
<?php

namespace App\Filters;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;

class Auth implements FilterInterface
{
    public function before(RequestInterface $request)
    {
        if(session()->get('logged_in') !== true)
        {
           return redirect()->to('login');
        }
    }


    //--------------------------------------------------------------------

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

  1. add your your new filter inside filters App/Config/Filters.php class to make it work
public $aliases = [
        'csrf'          => \CodeIgniter\Filters\CSRF::class,
        'toolbar'       => \CodeIgniter\Filters\DebugToolbar::class,
        'honeypot'      => \CodeIgniter\Filters\Honeypot::class,
        'auth'          => \App\Filters\Auth::class,
    ];
  1. in your Routes.php u can use that filter to spesify what route use run that filter before go to the route you want.

for single route

$routes->get('examplefunction', 'ControllerName::examplefunction', ['filter' => 'auth']);

for route group

$routes->group('', ['filter' => 'auth'], function ($routes) {
    $routes->get('/', 'ControllerName::examplefunction');
    $routes->get('examplefunction2', 'ControllerName::examplefunction2');
};

so that filter will executed before all single route inside the group run.

i hope this help you

Ashitaka
  • 560
  • 1
  • 6
  • 19
  • This is totally the correct answer. For login checks and redirection in case prevent access without login you should really use filters. Also in your controllers you shouldn't really use the __construct magic method but the __init one that already exists in your BaseController. – marcogmonteiro Sep 03 '20 at 08:14