• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the better solution for check if is logged in

#1
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.