Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I tend to roll my own simple authentication library. </p> <p>Firstly, this is the authentication library. It keeps a user ID token in the session. When authenticating it checks for the existence of this token.</p> <p><strong>application/libraries/Auth.php</strong></p> <pre><code>class Auth { var $ci; var $user_id; function Auth() { // Get CodeIgniter instance $this-&gt;ci = get_instance(); // Fetch token from the session $this-&gt;user_id = $this-&gt;ci-&gt;session-&gt;userdata('user_id'); } function check() { return $this-&gt;user_id != null; } function login($user_id) { // Set token in the session $this-&gt;ci-&gt;session-&gt;set_userdata('user_id', $user_id); $this-&gt;user_id = $user_id; } function logout() { // Remove token from the session $this-&gt;ci-&gt;session-&gt;unset_userdata('user_id'); $this-&gt;user_id = null; } } </code></pre> <p>I create my own base controller and authenticate there. For convenience, if authenticated, the base controller loads and stores the current user.</p> <p><strong>application/libraries/MY_Controller.php</strong></p> <pre><code>class MY_Controller extends Controller { var $user; function MY_Controller() { parent::Controller(); } function do_auth() { if ($this-&gt;auth-&gt;check()) { // Authenticated. Fetch current user $this-&gt;user = $this-&gt;user_model-&gt;get_user($this-&gt;auth-&gt;user_id); } else { // Not authenticated. Redirect to login page redirect('users/login'); } } } </code></pre> <p>Then in any action I can call the authentication function of the base controller.</p> <pre><code>class Items extends MY_Controller { function Items() { parent::MY_Controller(); } function create() { // Do authentication $this-&gt;do_auth(); // Continue with handling request } } </code></pre> <p>If I like I can also secure an entire controller.</p> <pre><code>class Items extends MY_Controller { function Items() { parent::MY_Controller(); // Secure entire controller $this-&gt;do_auth(); } } </code></pre> <p>I place the login and logout actions in a users controller. In the login action I verify the user's credentials and log in the user.</p> <pre><code>class Users extends MY_Controller { function Users() { parent::MY_Controller(); } function login() { // Verify form input $this-&gt;load-&gt;library('form_validation'); $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'required'); $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'required'); if ($this-&gt;form_validation-&gt;run()) { // Fetch the user based on credentials supplied $user = $this-&gt;user_model-&gt;get_user_by_credentials($this-&gt;input-&gt;post('username', true), $this-&gt;input-&gt;post('password', true)); if ($user != null) { // Credentials verified. Log the user in. $this-&gt;auth-&gt;login($user-&gt;user_id); redirect(''); } else { // Login failed. Show the login page. $this-&gt;load-&gt;view('users/login', array('login_failed' =&gt; true)); } } else { // Yet to authenticate. Show the login page. $this-&gt;load-&gt;view('users/login', array('login_failed' =&gt; false)); } } function logout() { $this-&gt;auth-&gt;logout(); redirect('users/login'); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload