Problem using Hooks to control page authorization

Asked

Viewed 26 times

1

I’m trying to use the Hooks of Codeigniter to prevent access to certain pages without being logged in. For this I enabled the Hooks and created in the archive application config Hooks.php the following structure to call the Hook.

$hook['post_controller_constructor'][] = array(
 'class'    => 'Authenticate',
 'function' => 'check_user_login',
 'filename' => 'Authenticate.php',
 'filepath' => 'hooks',
 'params'   => array()
);

Inside the folder of Hooks i created the Authenticate file.

application Hooks Authenticate.php

<?php
class Authenticate{
public function __construct() {}

function check_user_login() {
 $ci = & get_instance();
 $method = $ci->router->fetch_class().'/'.$ci->router->fetch_method(); /* Na minha pagina de login o retorno é Login/index */
 $protegidos = ['dashboard/index']; /* Setei para proteger no momento somente o dashboard */
 $usuario_logado = $ci->session->userdata('usuario_logado');
   if (in_array($method, $protegidos)) {
    if (!$usuario_logado[username]) {
        $ci->session->set_flashdata('alert', 'Autentique-se, por favor!');
        redirect(APP_URL . 'login');
    }
}
}
}
?>

But when I try to log in it redirects me directly to the login page again and does not enter the system.

  • Although I think that’s been answered (here), the problem is only what is recorded in SESSION 'user'. Of course, the hook is not finding the variable $usuario_logado[username].

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.