Welcome Guest, Not a member yet? Register   Sign In
Code Modules + Filters + Routes = ERR_TOO_MANY_REDIRECTS. Where am I going wrong?
#2

(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
 
   }

Reply


Messages In This Thread
RE: Code Modules + Filters + Routes = ERR_TOO_MANY_REDIRECTS. Where am I going wrong? - by adnzaki - 03-19-2019, 11:08 PM



Theme © iAndrew 2016 - Forum software by © MyBB