CodeIgniter Forums
Code Modules + Filters + Routes = ERR_TOO_MANY_REDIRECTS. Where am I going wrong? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Code Modules + Filters + Routes = ERR_TOO_MANY_REDIRECTS. Where am I going wrong? (/thread-73107.html)



Code Modules + Filters + Routes = ERR_TOO_MANY_REDIRECTS. Where am I going wrong? - ariom - 03-19-2019

Ok here we go. My folder structure looks like this:
[attachment=1459]

And before anyone asks, Yes I've added my namespace to app/Config/Autoload.php. That part works fine.
PHP Code:
$psr4 = [
    
'Config'      => APPPATH 'Config',
    
APP_NAMESPACE => APPPATH               // For custom namespace
    
'App'         => APPPATH               // To ensure filters, etc still found,
    
'AriomAuth'   => ROOTPATH 'Ariom/Auth',
]; 

What I want to do is basically roll-my-own auth system. I know there a plenty of choices even at this early stage of the beta, but I figured making my own would help wrap my head around CI4 and namespaces. So far so good.

So the entry point of the app is the Dashboard (app/Controllers/Dashboard.php). On its own, it works fine.
PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
Dashboard extends Controller {

    public function 
__construct() {

    }
    
    public function 
index() {    
        
helper('html');
        echo 
view('templates/header');
        echo 
view('Dashboard/Dashboard');
        echo 
view('templates/footer');
    }



I've also created a controller and view in my Auth module (Ariom/Auth/Controllers/Auth.php, and Ariom/Auth/Views/Login.php). On their own, they also works fine.
PHP Code:
<?php namespace AriomAuth\Controllers;

use 
CodeIgniter\Controller;

class 
Auth extends Controller {

    public function 
login() {
        echo 
view('AriomAuth\Views\Login');
    }



Where I'm having problems is when it comes to Routes and Filters. I want to redirect the user to login if there is no active session. The way I've seen it come up here in this forum a couple of times is to use a Filter. Ok great, so I've tried to have a go at this.

(Ariom/Auth/Filters/Auth.php)
PHP Code:
<?php namespace AriomAuth\Filters;

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

class 
Auth implements FilterInterface
{
 
   public function before(RequestInterface $request) {
 
       $session Services::session();
    if(!
$session->has('authenticated')) {
        return 
redirect('login');
    }
 
   }

 
   public function after(RequestInterface $requestResponseInterface $response {
 
       
    
}

(app/Config/Filters.php)
PHP Code:
//--code--

public $aliases = [
    
'csrf'     => \CodeIgniter\Filters\CSRF::class,
    
'toolbar'  => \CodeIgniter\Filters\DebugToolbar::class,
    
'honeypot' => \CodeIgniter\Filters\Honeypot::class,
    
'auth'     => \AriomAuth\Filters\Auth::class,
];


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

(Ariom/Auth/Config/Routes.php)
PHP Code:
<?php namespace AriomAuth\Config;
$routes->add('login''AriomAuth\Controllers\Auth::login'); 

So what I know is this:
  • the filter does run
  • the session is checked and correctly runs the redirect when no session variables are found
  • the redirect is triggered and loads the custom route
All good so far. BUT...

Although I do get redirected to /login, I time out with ERR_TOO_MANY_REDIRECTS. I have no idea where to begin debugging this. Any ideas, anyone?


RE: Code Modules + Filters + Routes = ERR_TOO_MANY_REDIRECTS. Where am I going wrong? - adnzaki - 03-19-2019

(03-19-2019, 08:14 PM)ariom Wrote: Ok here we go. My folder structure looks like this:


And before anyone asks, Yes I've added my namespace to app/Config/Autoload.php. That part works fine.
PHP Code:
$psr4 = [
    
'Config'      => APPPATH 'Config',
    
APP_NAMESPACE => APPPATH               // For custom namespace
    
'App'         => APPPATH               // To ensure filters, etc still found,
    
'AriomAuth'   => ROOTPATH 'Ariom/Auth',
]; 

What I want to do is basically roll-my-own auth system. I know there a plenty of choices even at this early stage of the beta, but I figured making my own would help wrap my head around CI4 and namespaces. So far so good.

So the entry point of the app is the Dashboard (app/Controllers/Dashboard.php). On its own, it works fine.
PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
Dashboard extends Controller {

    public function 
__construct() {

    }
    
    public function 
index() {    
        
helper('html');
        echo 
view('templates/header');
        echo 
view('Dashboard/Dashboard');
        echo 
view('templates/footer');
    }



I've also created a controller and view in my Auth module (Ariom/Auth/Controllers/Auth.php, and Ariom/Auth/Views/Login.php). On their own, they also works fine.
PHP Code:
<?php namespace AriomAuth\Controllers;

use 
CodeIgniter\Controller;

class 
Auth extends Controller {

    public function 
login() {
        echo 
view('AriomAuth\Views\Login');
    }



Where I'm having problems is when it comes to Routes and Filters. I want to redirect the user to login if there is no active session. The way I've seen it come up here in this forum a couple of times is to use a Filter. Ok great, so I've tried to have a go at this.

(Ariom/Auth/Filters/Auth.php)
PHP Code:
<?php namespace AriomAuth\Filters;

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

class 
Auth implements FilterInterface
{
 
   public function before(RequestInterface $request) {
 
       $session Services::session();
    if(!
$session->has('authenticated')) {
        return 
redirect('login');
    }
 
   }

 
   public function after(RequestInterface $requestResponseInterface $response {
 
       
    
}

(app/Config/Filters.php)
PHP Code:
//--code--

public $aliases = [
    
'csrf'     => \CodeIgniter\Filters\CSRF::class,
    
'toolbar'  => \CodeIgniter\Filters\DebugToolbar::class,
    
'honeypot' => \CodeIgniter\Filters\Honeypot::class,
    
'auth'     => \AriomAuth\Filters\Auth::class,
];


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

(Ariom/Auth/Config/Routes.php)
PHP Code:
<?php namespace AriomAuth\Config;
$routes->add('login''AriomAuth\Controllers\Auth::login'); 

So what I know is this:
  • the filter does run
  • the session is checked and correctly runs the redirect when no session variables are found
  • the redirect is triggered and loads the custom route
All good so far. BUT...

Although I do get redirected to /login, I time out with ERR_TOO_MANY_REDIRECTS. I have no idea where to begin debugging this. Any ideas, anyone?

I have implemented such this and it runs well with this chunk of codes.
PHP Code:
<?php namespace Actudent\Admin\Filters;

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

class 
AdminFilter implements FilterInterface
{
 
   public function before(RequestInterface $request)
 
   {
 
       $session Services::session();
 
       if($session->get('email') === null || $session->get('userLevel') !== '1')
 
       {
 
           return redirect()->to(site_url('admin/login'));
 
       }
 
   }

 
   public function after(RequestInterface $requestResponseInterface $response)
 
   {
 
       // Do something here
 
   }




RE: Code Modules + Filters + Routes = ERR_TOO_MANY_REDIRECTS. Where am I going wrong? - adnzaki - 03-19-2019

itu looks like your "before" filter could not find true condition, so it redirected many times.


RE: Code Modules + Filters + Routes = ERR_TOO_MANY_REDIRECTS. Where am I going wrong? - ariom - 03-19-2019

I solved about 30 seconds after I hit 'Post Topic'.  Blush


In App/Config/Filters.php where I specify the filter is this:
PHP Code:
// Always applied before every request
public $globals = [
    
'before' => [
        
//'honeypot'
        // 'csrf',
        
'auth',
//...
//... 
Note the comment "Always applied before every request". I realised this was being run on the Dashboard (where it is meant to), and AGAIN on the /login page (which redirects it again...and again...and again...). Oops. I changed it to this and it works now:
PHP Code:
// Always applied before every request
public $globals = [
    
'before' => [
        
//'honeypot'
        // 'csrf',
        
'auth' => ['except' => 'login'],
//...
//... 



RE: Code Modules + Filters + Routes = ERR_TOO_MANY_REDIRECTS. Where am I going wrong? - GuilhermeBiz - 03-20-2019

Thanks for sharing and solving your own issue, also thats a simple and efective way of doing a very simple authentication.