Skip to content

Instantly share code, notes, and snippets.

@techies23
Created February 1, 2016 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techies23/f7b302cecc08cb2a4f50 to your computer and use it in GitHub Desktop.
Save techies23/f7b302cecc08cb2a4f50 to your computer and use it in GitHub Desktop.
Authentication File
class Auth extends Admin_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library(array('ion_auth'));
$this->load->helper(array('url','language'));
$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
$this->lang->load('auth');
}
// redirect if needed, otherwise display the user list
function index()
{
if (!$this->ion_auth->logged_in())
{
// redirect them to the login page
redirect('auth/login', 'refresh');
}
elseif (!$this->ion_auth->is_admin()) // remove this elseif if you want to enable this for non-admins
{
// redirect them to the home page because they must be an administrator to view this
return show_error('You must be an administrator to view this page.');
}
else
{
// // set the flash data error message if there is one
// $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
// //list the users
// $this->data['users'] = $this->ion_auth->users()->result();
// foreach ($this->data['users'] as $k => $user)
// {
// $this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
// }
// $this->_render_page('auth/index', $this->data);
redirect('admin/dashboard', 'refresh');
}
}
// 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('/admin/dashboard', '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('auth/login', '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',
);
//$this->_render_page('auth/login', $this->data);
$this->data['subview'] = 'auth/login';
$this->load->view('templates/_layout_login', $this->data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment