Welcome Guest, Not a member yet? Register   Sign In
What is the better solution for check if is logged in
#1

(This post was last modified: 04-01-2021, 11:19 PM by cvlancvlan.)

What is the better solution for check if is logged in ?

app/Controllers/Back/DashbardController.php
PHP Code:
<?php namespace App\Controllers\Back;
use 
App\Controllers\Back\BackController;

class 
DashbardController extends BackController {
    
    public function list() {
        
        echo 
"You can see this content just if you are logged";
        
    }
    


app/Controllers/Back/BackController.php ( Method 1 )
PHP Code:
<?php namespace App\Controllers\Back;
use 
App\Controllers\BaseController;

class 
BackController extends BaseController {
    
    public function 
initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger) {
        
        
parent::initController($request$response$logger);
        
        if ( !
service('authLibrary')->isLoggedIn() ) {
            
            
header("HTTP/1.1 301 Moved Permanently"); 
            
header('Location: ' base_url('login'));
            exit;
            
        }
        
    }
    


app/Libraries/AuthLibrary.php
PHP Code:
<?php namespace App\Libraries;

class 
AuthLibrary {

    public function isLoggedIn() {
        
        
return FALSE;
        
    }
    


app/Config/Services.php
PHP Code:
<?php namespace Config;
use 
CodeIgniter\Config\Services as CoreServices;
require_once 
SYSTEMPATH 'Config/Services.php';

class 
Services extends CoreServices {
    
    public static function authLibrary($getShared=false){
        if($getShared){
            return self::getSharedInstance('authLibrary');
        }  return new \App\Libraries\AuthLibrary();
    }



app/Filters/LoginFilter.php
PHP Code:
<?php namespace App\Filters;
use 
CodeIgniter\Filters\FilterInterface;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;

class 
LoginFilter implements FilterInterface {
    
   public function before(RequestInterface $request) {
       
        $authLibrary 
service('authLibrary');

        if ( !
$authLibrary->isLoggedIn() ) {
          
            header
("HTTP/1.1 301 Moved Permanently"); 
            
header('Location: ' base_url('login'));
            exit; 
        
  
        
}
    
   
   
}
   
   
public function after(RequestInterface $requestResponseInterface $response) {
       
   
}  
   


app/Config/Filters.php ( Method 2 )
PHP Code:
<?php namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
Filters extends BaseConfig {
    
    public 
$aliases = [
        
'csrf'     => \CodeIgniter\Filters\CSRF::class,
        
'toolbar'  => \CodeIgniter\Filters\DebugToolbar::class,
        
'honeypot' => \CodeIgniter\Filters\Honeypot::class,
        
'loginFilter' => \App\Filters\LoginFilter::class,
    ];

    public 
$globals = [
        
'before' => [
            
//'honeypot'
            //'csrf',
            //'loginFilter',
        
],
        
'after'  => [
            
'toolbar',
            
//'honeypot'
        
],
    ];

    public 
$methods = [];

    public 
$filters = [
        
'loginFilter' => ['before' => ['back/*']]
    ];
    

Reply
#2

I'm a fan of filters for authentication (and really any route "filtering"). Note that you can return a RedirectResponse from Filter::before() to let the framework handle the redirect for you - much easier to testing and clean up and logging then using `header()` + `exit()`
Reply




Theme © iAndrew 2016 - Forum software by © MyBB