I have Ion Auth properly installed and working on my server. I also have the default CodeIgniter 2 "news" tutorial working in the same CI installation. I'm just playing around and curious about the proper way to use the authentication system to "enclose" or protect an entire application.
For this question, let's use the "news" tutorial that comes with CI.
Inside the index()
function in my news.php
controller, I added conditional code to check if the user is logged in. If not, the user is just taken to the login screen.
public function index() {
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
if ($this->ion_auth->logged_in()) {
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
} else {
redirect('auth/login', 'refresh');
}
}
I can see this works, but the immediate downside is that every function within the controller would also have to be modified with similar conditional logic to protect all other page views. e.g. - check for login, display page, else go to login page... over and over.
Is this the way it's supposed to be done?
What if an application is already built and working and one simply wants to protect it all? Adding conditional logic to check login status on every single page view within the controller seems unnecessarily verbose.
Can the whole application (all views) be protected in one place to minimize code modification? If so, how?