-
ariom Knows PHP/CI enough to be dangerous

-
Posts: 2
Threads: 1
Joined: Mar 2019
Reputation:
0
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 $request, ResponseInterface $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?
-
adnzaki Junior Member
 
-
Posts: 21
Threads: 1
Joined: Jan 2019
Reputation:
0
(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 $request, ResponseInterface $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 $request, ResponseInterface $response) { // Do something here } }
-
ariom Knows PHP/CI enough to be dangerous

-
Posts: 2
Threads: 1
Joined: Mar 2019
Reputation:
0
I solved about 30 seconds after I hit 'Post Topic'.
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'], //... //...
|