CodeIgniter Forums
Ion Auth - Lightweight Auth System based on Redux Auth 2 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Ion Auth - Lightweight Auth System based on Redux Auth 2 (/showthread.php?tid=27435)



Ion Auth - Lightweight Auth System based on Redux Auth 2 - El Forum - 04-12-2010

[eluser]mckyja[/eluser]
Hi all, just getting started with CI and enjoying it (been years since i've done web stuff ).

I have been tasked with creating a simple web based employee management system for the HR department, and people need to be logged in at all times to view information. Ion_Auth seems to fit that need well.

Where would be the best place to put a kind of 'global' logged_in() check?

Is my understanding correct that extending the Controller with MY_Controller will provide that check every time any controller is called with something like this?
Code:
<?php

class MY_Controller extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('ion_auth');
        $this->load->library('session');
        if (!$this->ion_auth->logged_in()) {
            //redirect them to the login page
            redirect('auth/login', 'refresh');
        }
    }
}

Thanks in advance,
Kyle.


Ion Auth - Lightweight Auth System based on Redux Auth 2 - El Forum - 04-12-2010

[eluser]Ben Edmunds[/eluser]
Kyle,

That is the correct way to do it.

Good job man, hope you continue to enjoy working with CI and Ion Auth.


Ion Auth - Lightweight Auth System based on Redux Auth 2 - El Forum - 04-12-2010

[eluser]mckyja[/eluser]
Thanks for the quick reply Ben, great community here from what i've seen Smile

One other quick question for you, how can I use Ion to display User ID and first/last name.

(Sorry if this is a simple one, still getting up to speed on php and CI)

Thanks,
Kyle


Ion Auth - Lightweight Auth System based on Redux Auth 2 - El Forum - 04-12-2010

[eluser]Ben Edmunds[/eluser]
Kyle,

You can just do

Code:
$user = $this->ion_auth->get_user();

To get the user object and then you can access the properties like so

Code:
echo $user->id .' - '. $user->first_name .' '.  $user->last_name;

and of course you can pass the $user object to your view and echo the properties there.


Ion Auth - Lightweight Auth System based on Redux Auth 2 - El Forum - 04-12-2010

[eluser]mckyja[/eluser]
thanks Ben. much appreciated.


Ion Auth - Lightweight Auth System based on Redux Auth 2 - El Forum - 04-12-2010

[eluser]Zen Savona[/eluser]
I have a small problem with this. i am quite new to CI, but loving it so far. I installed it per the instructions on Github, and now when i try to access the auth controller from my browser it is redirecting me wrongly.

my install is localhost/AGN, therefore to access the auth demo, i would go to localhost/AGN/auth. when i go there it redirects me to localhost/AGN/localhost/AGN/auth/login, if i manually go to localhost/AGN/auth/login i can enter my credentials but then it redirects me to localhost/AGN/localhost/AGN/auth/login again on submit.



Hope you can help me fix this, as your tool looks extremely promising.


Ion Auth - Lightweight Auth System based on Redux Auth 2 - El Forum - 04-12-2010

[eluser]Ben Edmunds[/eluser]
Zen,

Does the standard welcome controller work properly?


Ion Auth - Lightweight Auth System based on Redux Auth 2 - El Forum - 04-12-2010

[eluser]Zen Savona[/eluser]
[quote author="Ben Edmunds" date="1271148101"]Zen,

Does the standard welcome controller work properly?[/quote]


Yes, the welcome controller works fine, but it is not the default controller to be displayed.


Ion Auth - Lightweight Auth System based on Redux Auth 2 - El Forum - 04-12-2010

[eluser]Ben Edmunds[/eluser]
Zen,

Check the redirects.


Ion Auth - Lightweight Auth System based on Redux Auth 2 - El Forum - 04-12-2010

[eluser]Zen Savona[/eluser]
Thats the odd part, it all seems correct.
Code:
function index()
    {
        if (!$this->ion_auth->logged_in()) {
            //redirect them to the login page
            redirect('auth/login', 'refresh');
        }
        elseif (!$this->ion_auth->is_admin()) {
            //redirect them to the home page because they must be an administrator to view this
            redirect($this->config->item('base_url'), 'refresh');
        }
        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->get_users_array();
            $this->load->view('auth/index', $this->data);
        }
    }


when i go to localhost/AGN/auth, because i am not logged in, it should redirect me to auth/login. which is localhost/AGN (base url)/auth/login


...makes no sense to me