Welcome Guest, Not a member yet? Register   Sign In
Best way to create user authentication
#1

[eluser]Gukkie[/eluser]
Hi, i just wanan know wat's the best way to create a session/cookie authentication.

Shud i create a library or a helper to access the database to create a session if a cookie is detected or to redirect the user to the members area if a session is detected?

Currently im using a model and im calling it on every controller and i think its not feasable doing it this way.

Cheers!
#2

[eluser]PhilTem[/eluser]
Best would be to create a library that is accessed from every controller since models are designed or intended to only talk to the database - even though there is a pretty loose interpretation of the MVC-pattern in CI.
The library than packs stuff together from necessary model(s) and helper(s) to create the actions required and to provide appropriate and needed information.

And don't forget you can create different types of "default controllers" by extending CI_Controller. Create a MY_Controller, that'll load you auth-library, create a Registered_Controller that checks in the constructor whether the session for a logged in user is set or not. Makes life so much easier Wink
#3

[eluser]Gukkie[/eluser]
Hi buddy, thanks for the help, i dont quite get how u make the controller part work, any examples on how to do it?

This is my code currently on my "User" controller as well as my "Site" controller and my "User" model.

User controller(handles pages before login):

Code:
function __construct()
{
  parent::__construct();
  $this->load->model('User_model');
  
  if(get_cookie('mysite') || $this->User_model->is_logged_in())
  {
   redirect('site');
  }
  
  $this->load->library(array('email', 'parser'));
}

Site controller(handles pages when logged in):

Code:
function __construct()
{
  parent::__construct();
  $this->load->model('User_model');
  
  if(get_cookie('mysite') && !$this->User_model->is_logged_in())
  {
   $this->User_model->get_sess_from_cookie();
  }
  elseif(!$this->User_model->is_logged_in() || $this->User_model->is_logged_in() != true)
  {
   redirect('user/login');
  }
}

User model(handles all db stuff before login):

Code:
function is_logged_in()
{
  $is_logged_in = $this->session->userdata('is_logged_in');
  
  if(!isset($is_logged_in) || $is_logged_in != true)
  {
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}

function get_sess_from_cookie()
{
  $cookie = get_cookie('mysite');
  $value = unserialize($cookie);
  
  $id    = $value['id'];
  $username = $value['username'];
  
  $this->db->where('id', $id);
  $this->db->where('username', $username);
  $query = $this->db->get('user');
  
  if($query->num_rows == 1)
  {
   foreach($query->result() as $row)
   {
    $id    = $row->id;
    $username = $row->username;
    $email    = $row->email;
    
    $sessionData = array(
      'id'      => $id,
      'username'     => $username,
      'email'     => $email,
      'is_logged_in' => true
      );
    $this->session->set_userdata($sessionData);
   }
  }
}

as u can see, im calling the User_model in every controller just to varify if the session exists.




Theme © iAndrew 2016 - Forum software by © MyBB