[eluser]Unknown[/eluser]
hey guys,
i have a question regarding the authentication library Ion_Auth and how to implement it the best way.
So what i have is the Ion_Auth library and the auth controller. The problem i dont want to use the auth controller is, that i will need the login, logoff, register functions etc. on multiple sites and therefore controllers. So what i did now is to write a MY_Controller.php.
It looks like the following:
Code:
class Admin_Controller extends Auth_Controller {
protected $the_user;
public function __construct() {
parent::__construct();
if ($this->ion_auth->is_admin()) {
$this->the_user = $this->ion_auth->user()->row();
$data->the_user = $this->the_user;
$this->load->vars($data);
} else {
redirect('/');
}
}
}
class User_Controller extends Auth_Controller {
protected $the_user;
public function __construct() {
parent::__construct();
if ($this->ion_auth->FALSE) {
$this->the_user = $this->ion_auth->user()->row();
$data->the_user = $this->the_user;
$this->load->vars($data);
} else {
redirect('/');
}
}
}
class Public_Controller extends Auth_Controller {
protected $the_user;
public function __construct() {
parent::__construct();
if ($this->ion_auth->logged_in()) {
if ($this->ion_auth->FALSE) {
redirect('user/home');
} else if ($this->ion_auth->is_admin()) {
redirect('admin/home');
} else {
redirect('user/home');
}
}
}
}
/**
* The Authentication Controller
*
* Provides fucntions for login, logout, forgot password etc.
*
*/
class Auth_Controller extends CI_Controller {
function __construct() {
parent::__construct();
// Load MongoDB library instead of native db driver if required
$this->config->item('use_mongodb', 'ion_auth') ?
$this->load->library('mongo_db') :
$this->load->database();
$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
}
//redirect if needed, otherwise display the user list
function index() {
}
//log the user in
function login() {
$this->data['title'] = "Login";
//validate form input
$this->form_validation->set_rules('identity', 'Identity', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == true) {
//check to see if the user is logging in
//check for "remember me"
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember)) {
//if the login is successful
//redirect them back to the home page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect('/', 'refresh');
} else {
//if the login was un-successful
//redirect them back to the login page
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('/', 'refresh'); //use redirects instead of loading views for compatibility with MY_Controller libraries
}
} else {
//the user is not logging in so display the login page
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->data['identity'] = array('name' => 'identity',
'id' => 'identity',
'type' => 'text',
'value' => $this->form_validation->set_value('identity'),
);
$this->data['password'] = array('name' => 'password',
'id' => 'password',
'type' => 'password',
);
$login = $this->load->view('templates/auth/login', $this->data, TRUE);
return $login;
}
}
}
So each "normal" controller extends the Public_Controller which checks if the user is logged_in and maybe an admin and then redirects the client to the fitting controller. No each of the controllers in MY_Controller.php is extended of Authentication_Controller. With this approach i now can be sure that every controller can access the autentication features.
I really do not know if this is a good approach and maybe you could help me to find one. It was really hard to find one on the internet.
Kind Regards
LionKrieger