• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TUTORIAL : simple side wide login

#1
[eluser]xwero[/eluser]
I'm back with a tutorial on how to use hooks in a creative manner. This time i'm going to demonstrate a simple authentication with side wide login form.

The common way to do this is to extend the controller and place the intercepting code there but with hooks you don't have to extend the controllers.

Code:
function login_form()
{
   $CI =& get_instance();
   $CI->load->vars(array('login',$CI->load->view('login_form'));
}

function authenticate()
{
   if(isset($_POST['login_button']))
   {
      $CI =& get_instance();
      $CI->load->model('auth_model','',TRUE);
      if($CI->auth_model->check($_POST['username'],$_POST['password']))
      {
        $CI->session->set_userdata('id',$CI->auth_model->get_id($_POST['username'],$_POST['password']));
      }
   }
}

function check_authentication()
{
   $CI =& get_instance();
   if($CI->session->userdata('id'))
   {
       $CI->load->model('users_model','',TRUE);
       $CI->load->vars(array('login',$CI->load->view('logged_in',$CI->users_model->logged_in($CI->session->userdata('id')));
   }
}
This is the content of the hooks/auth.php file.

I'm not putting the functions into a class because you will never write the functions yourself in controller code. Once namespaces are available i would add them to a namespace to minimize the risk of having a function with the same name.

The config/hooks.php looks like this
Code:
$hook['post_controller_constructor'][] = array(
                                'function' => 'login_form',
                                'filename' => 'auth.php',
                                'filepath' => 'hooks',
                                );

$hook['post_controller_constructor'][] = array(
                                'function' => 'authenticate',
                                'filename' => 'auth.php',
                                'filepath' => 'hooks',
                                );

$hook['post_controller_constructor'][] = array(
                                'function' => 'check_authentication',
                                'filename' => 'auth.php',
                                'filepath' => 'hooks',
                                );

The placing of the hook functions is important. The first two hooks can be switched but the third has to be last.

And that is it. Now you have a reusable, basic authentication solution.

#2
[eluser]joneslee[/eluser]
If possible, can you post up your auth_model and users_model code?

#3
[eluser]The Wizard[/eluser]
by side wide login you exactly mean?

#4
[eluser]GSV Sleeper Service[/eluser]
load-vars() is new to me, must have missed that when reading the docs.
Quote:I’m not putting the functions into a class because you will never write the functions yourself in controller code.
I don't understand this line, why wouldn't I?

[edit] - about this load->vars() thing. shouldn't your code look like this, or am I completely misunderstanding how load->vars() works?
Code:
function login_form()
{
   $CI =& get_instance();
   $CI->load->vars(array('login',$CI->load->view('login_form', '', true));
}
which to me says "load the login_form view, return it, then assign it to a var called 'login'"


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.