CodeIgniter Forums

Full Version: How to check if a user logged in?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can check and restrict access to certain methods or controllers by defining a helper method
PHP Code:
function checkLoggedIn() {
 
   if (session('loggedin') == true) {
 
       return true;
 
   }
 
   header("Location: /login");
 
   exit;


Then an example usage is as below.

PHP Code:
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
Home extends Controller
{
 public function 
index()
 {
 return 
view('welcome_message');
 }

 
   public function showme($page 'home')
 
   {
 
       checkLoggedIn();
 
   }
}

class 
AdminHome extends Controller
{
 
   public function __construct()
 
   {
 
       checkLoggedIn();
 
   }

 
   public function index()
 {
 return 
view('welcome_message');
 }


Do you have any better method than this helper method? E.g. in laravel there are guards. Could I achieve something similar without having to write too much of code?
My approach is with Filters:

PHP Code:
//app/Config/Filters.php
public $aliases = [
 
   'auth' => \App\Filters\Auth::class,
];
public 
$globals = [
 
   'before' => [
        'auth',
     ],
]; 
PHP Code:
//app/Filters/Auth.php
namespace App\Filters;
use 
CodeIgniter\Filters\FilterInterface;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Config\Services;

class 
Auth implements FilterInterface
{
 
   public function before(RequestInterface $request)
 
   {
    
$session Services::session();
 
       if ($session->has('auth'))
 
       
 
           if ($request->uri->getPath() == 'auth/login')
 
           {
 
               return redirect()->to('auth/profile');
 
           }
 
           if ($request->uri->getSegment(1) == 'admin')
 
           {
 
                return redirect()->back();
 
           }
 
       
 
       else
        
{
 
           if ($request->uri->getPath() != 'auth/login')
 
           {
 
               return redirect()->to('auth/login');
 
           }
 
       }
 
   }

 
   public function after(RequestInterface $requestResponseInterface $response)
 
   {
 
   }


Thank you for sharing your solution @elephpantech.

PHP Code:
//app/config/filters.php
    // List filter aliases and any before/after uri patterns
    // that they should run on, like:
    //    'isLoggedIn' => ['before' => ['account/*', 'profiles/*']],
    
public $filters = []; 
It seems like I can target all (global) requests or selected routes - however, there isn't any way to target a specific controller using filter?

I think I need to look into services for that purpose perhaps?
Actually I can apply filter directly in the routes.php
PHP Code:
$routes->group('api', ['filter' => 'api-auth'], function($routes)
{
    
$routes->resource('users');
}); 
So, that's useful as I don't have to specify routes in $filters in config/filters.php
2 Filters Class: Authentication and Authorization
PHP Code:
public $globals = [
 
   'before' => [
 
       'authentication' => ['except' => ['users/login']],
 
   ],
];
public 
$filters = [
 
   'authorization' => ['before' => ['admin/*']],
];