Welcome Guest, Not a member yet? Register   Sign In
About IonAuth 4 auth System
#1
Sad 
(This post was last modified: 11-02-2019, 10:03 PM by adelbak.)

I'm working on a project based on codeigniter 4 and IonAuth
- I used a IonAuth library as Auth System and included it in BaseController successfully using:
$this->ionAuth = new \IonAuth\Libraries\IonAuth();
- After that i make my Auth controller extends BaseController without problems.

The question is:

When I check the login in __construct function of any controller I get Call to a member function loggedIn() on null error.
-  when i add $this->ionAuth = new \IonAuth\Libraries\IonAuth(); in __construct function even though I called him in BaseController before, Error is gone but loggedIn() don't work Huh .


PHP Code:
<?php namespace App\Controllers;

use 
App\Models\DashboardModel;

class 
Dashboard extends BaseController
{
    
    protected 
$Dashboard;

    public function 
__construct()
    {
    
    $this->dashboardModel = new DashboardModel();
            $this->ionAuth = new \IonAuth\Libraries\IonAuth();        

        if (!
$this->ionAuth->loggedIn())
        {
            return redirect()->to(base_url().'/auth/login');
        }
        
    }    
    
    public function indexOne()
    {
        return view('pages/indexOne');       
    
}

    public function indexTwo()
    {
        return view('pages/IndexTwo');       
    
}
    
    public function indexThree()
    {
        return view('pages/indexThree');       
    
}




To avoid the error I have to call if (!$this->ionAuth->loggedIn()) at all controll methodes like this:
PHP Code:
<?php namespace App\Controllers;

use 
App\Models\DashboardModel;

class 
Dashboard extends BaseController
{
    
    protected 
$Dashboard;

    public function 
__construct()
    {
    
    $this->dashboardModel = new DashboardModel();
    }    
    
    public function indexOne()
    {        
    
    if (!$this->ionAuth->loggedIn())
        {
            return redirect()->to(base_url().'/auth/login');
        }
        return view('pages/indexOne');       
    
}

    public function indexTwo()
    {
    
    if (!$this->ionAuth->loggedIn())
        {
            return redirect()->to(base_url().'/auth/login');
        }    
        return view('pages/IndexTwo');       
    
}
    
    public function indexThree()
    {
    
    if (!$this->ionAuth->loggedIn())
        {
            return redirect()->to(base_url().'/auth/login');
        }    
        return view('pages/indexThree');       
    
}




I think that's ugly way, any Solutions please??
Thanks.
Reply
#2

I believe that you are making some confusion...

Try to keep it simple...

Just load the library in method and BE HAPPY ! Ion Auth is meant to be simple...  Big Grin

PHP Code:
    public function addNewRecord()
    {

        //Loads the Authentication Library: ION AUTH
        $ionAuth = new \IonAuth\Libraries\IonAuth();

        //Checks if the user is Logged In
        if(!$ionAuth->loggedIn()){

           die;   

        }
    
Reply
#3

I try create full support with CI4: https://github.com/benedmunds/CodeIgnite.../pull/1384
Reply
#4

(11-09-2019, 02:08 PM)Poetawd Wrote: I believe that you are making some confusion...

Try to keep it simple...

Just load the library in method and BE HAPPY ! Ion Auth is meant to be simple...  Big Grin

PHP Code:
    public function addNewRecord()
    {

        //Loads the Authentication Library: ION AUTH
        $ionAuth = new \IonAuth\Libraries\IonAuth();

        //Checks if the user is Logged In
        if(!$ionAuth->loggedIn()){

          die;  

        
}
    

But suppose you have logged in and that you want to send a message notifying the user that he does not have access to certain system functionality.

Inicio Controller:

PHP Code:
public function ErrorDeAcceso(){
 
 
$mostrar["error"] = "ACCESO DENEGADO, SOLICITE AYUDA A UN ADMINISTRADOR.";
 
$mostrar["contenido"] = "vista_acceso_denegado";
 return 
view("plantilla"$mostrar);
 
    

Usuarios Controller:
PHP Code:
public function VerSiUsuarioTieneAccesoAlaOpcion($id_modulo_opcion){
 
//esta funcion verifica si el usuario tiene permsios para acceder a la accion recibe como paramentro el id_modulo_opcion si el usuario no tiene permissos le redirige a una vista con un msj que no tiene acceso a tal proceso
 
if (!in_array("$id_modulo_opcion"$this->la_sesion->ids_modulo_opciones)) {
 
 
////base_url()."Inicio/ErrorDeAcceso";
 
return redirect()->to(base_url()."/Inicio/ErrorDeAcceso/");
 exit;
 }
 }
public function 
ListaUsuarios(){
 
//verifico si el usuario tiene permisos a esta opcion enviando el id_modulo_opcion que es unica
 
$this->VerSiUsuarioTieneAccesoAlaOpcion(700);
 
$mostrar["ListaUarios"] = $this->Usuariosbuscar->ListaUarios();
 
$mostrar["contenido"] = "usuarios/vista_usuarios";
 return 
view('plantilla'$mostrar);
 } 

When invoking
PHP Code:
$this->VerSiUsuarioTieneAccesoAlaOpcion(700); 

I did not redirect myself to the respective controller, but it continues to execute what follows after
PHP Code:
$this->VerSiUsuarioTieneAccesoAlaOpcion(700); 

something wrong?
Reply
#5

You have to create a filter in App -> Filters, something like :
PHP Code:
<?php

namespace App\Filters;

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

/**
* Description of AuthFilter
*
* @author christian
*/
class AuthFilter implements FilterInterface {

    public function before(RequestInterface $request) {
        $this->ionAuth = new IonAuth();
        if (!($this->ionAuth->loggedIn()) || !($this->ionAuth->isAdmin())) {
            session()->set('redirect_url'current_url());
            return redirect()->to('auth/login');            
        
}
    }

    //--------------------------------------------------------------------

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



Then to apply it, modify the config in App -> Config -> Filters like this :
PHP Code:
[quote pid='369076' dateline='1572757201']
<?
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,
 'auth'    => \App\Filters\AuthFilter::class
 ];

// Always applied before every request
public $globals = [
'before' => [
   'auth' => ['except'=>['auth/login']],
//'honeypot'
// 'csrf',
],
'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 = [];
}
[/
quote
Reply
#6

(11-02-2019, 10:00 PM)adelbak Wrote: - After that i make my Auth controller extends BaseController without problems.

Hi everybody. Is there anyone here who can clarify me the need of creating my own Auth controller extending the Basecontroller? Just to customize views?

Thanks in advance!
[Image: https://vulndetect.org/assets/uploads/fi...docker.ico]

Andy Biancoblu 

Quote:Once you have eliminated the impossible, whatever remains, however improbable, must be the truth.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB