Welcome Guest, Not a member yet? Register   Sign In
Help implimenting filters for redirects
#1

Hello.  There is obviously something I am not understanding here.  I am trying to do basically what was in this thread 'Redirection in a helper' 
I made this filter
Code:
<?php


namespace App\Filters;


use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use IonAuth\Libraries\IonAuth;

class LoginFilter implements FilterInterface
{
    /**
    * IonAuth library
    *
    * @var IonAuth
    */
    private $ionAuth;

    public function before(RequestInterface $request)
    {
        $this->ionAuth = new IonAuth();

        if (! $this->ionAuth->loggedIn())
        {
            // redirect them to the login page
            return redirect()->to('Home');
        }
    }

    public function after(RequestInterface $request, ResponseInterface $response)
    {
        // Do something here
    }
}

In app/Config/Filters.php I did this
Code:
<?php namespace Config;

use CodeIgniter\Config\BaseConfig;

class Filters extends BaseConfig
{
// Makes reading things below nicer,
// and simpler to change out script that's used.
public $aliases = [
'csrf'    => \CodeIgniter\Filters\CSRF::class,
'toolbar'  => \CodeIgniter\Filters\DebugToolbar::class,
'honeypot' => \CodeIgniter\Filters\Honeypot::class,
        'loggedin' => \App\Filters\LoginFilter::class
];

// Always applied before every request
public $globals = [
'before' => [
//'honeypot'
// 'csrf',
            'loggedin'

],
'after'  => [
//'toolbar',
//'honeypot'
],
];

// Works on all of a particular HTTP method
// (GET, POST, etc) as BEFORE filters only
//    like: 'post' => ['CSRF', 'throttle'],
public $methods = [];

// List filter aliases and any before/after uri patterns
// that they should run on, like:
//    'isLoggedIn' => ['before' => ['account/*', 'profiles/*']],
public $filters = [
    ];
}

A block of the Home controller
Code:
<?php namespace App\Controllers;

/**
* @abstract This is the mains controller for the TriLIMS Site.
* @author Dennis A. Simpson
* @copyright April 2019
*
*/

class Home extends TriLIMS
{
    /**
    *
    * @var array
    */
    public $data = [];

    /**
    * Views folder
    * Set it to 'auth' if your views files are in the standard application/Views/auth
    *
    * @var string
    */
    protected $viewsFolder = '\auth';

    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {
        if (! $this->ionAuth->loggedIn())
        {
            // redirect them to the login page
            return redirect()->to('Home/login');
        }
        $this->display('HomePage', $this->session->getFlashdata('message'), '', '', '', '', '', "", 'Home', 'Main Page');

    }

The TriLims controller
Code:
<?php

namespace App\Controllers;
/**
* Created by PhpStorm.
* User: pkMyt1
* Date: 10/23/2018
* Time: 9:19 AM
*/

use CodeIgniter\Controller;
use Config\Services;
use IonAuth\Libraries\IonAuth;

class TriLIMS extends Controller
{

    public $smarty;

    /**
    * Validation library
    *
    * @var \CodeIgniter\Validation\Validation
    */
    public $validation;

    /**
    * Session
    *
    * @var \CodeIgniter\Session\Session
    */
    public $session;

    /**
    * IonAuth library
    *
    * @var IonAuth
    */
    public $ionAuth;

    /**
    * Configuration
    *
    * @var \IonAuth\Config\IonAuth
    */
    protected $configIonAuth;

    public function __construct()
    {
        $this->smarty = Services::smarty();
        $this->validation = Services::validation();
        $this->session = Services::session();
        $this->ionAuth    = new IonAuth();
        $this->configIonAuth = config('IonAuth');
        helper(['form','cookie', 'date']);
    }

    public function display(string $template, $success, string $actionMode, $errors, $data, $id, $pager, $fields, $controller, $title)
    {
        $this->configIonAuth->minPasswordLength;
        if ($this->ionAuth->loggedIn())
        {
            $this->smarty->assignByRef('isAdmin', $this->ionAuth->isAdmin());
            $this->smarty->assignByRef('userID', $this->ionAuth->getUserId());
        }

        $this->smarty->assignByRef('db_fields', $fields);
        $this->smarty->assignByRef('record_id', $id);
        $this->smarty->assignByRef('controller', $controller);

        $this->smarty->assignByRef('title', $title);
        $this->smarty->assignByRef('pager', $pager);
        $this->smarty->assignByRef('action_mode', $actionMode);
        $this->smarty->assignByRef('success', $success);
        $this->smarty->assignByRef('errors', $errors);
        $this->smarty->assignByRef('data', $data);

        $this->smarty->assign('left_nav', 'LeftNav');
        $this->smarty->assign('header', 'Header');
        $this->smarty->assign('footer','Footer');
        $this->smarty->assignByRef('template', $template);

        echo $this->smarty->view('MainFrame.tpl');
    }
}

When I try this I get "The page isn't redirecting properly"  I am at a loss.  There is something I am doing wrong with the filter but I don't understand what.  The redirect code works if I remove the filter but I think I should be filtering so someone not logged in cannot jump into one of the controllers.
Reply
#2

I believe that’s the for message when your redirect isn’t routable. Have you tried “home” or “/home” instead of “Home”?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB