yowza
yowza

Reputation: 260

CodeIgniter and IonAuth 'If user is logged in'

I'm currently working on a CodeIgniter and IonAuth project and I have encountered this problem.

Scenario: When I login, I can still access the login page. What I want to do is to remove this access because the user is already logged in.

this is my index method in my auth class:

function index(){

        if (!$this->ion_auth->logged_in())
        {
            //redirect them to the login page
            redirect('auth/login', 'refresh');
        }
        elseif($this->ion_auth->logged_in())
        {
            //redirect them to the index page if logged in
            redirect('/', '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');
        }
}

Any ideas would be awesome!

Upvotes: 0

Views: 1034

Answers (1)

Timothy Zorn
Timothy Zorn

Reputation: 3231

Open up the auth controller. Go down to the login method. Add the following lines at the top:

if($this->ion_auth->logged_in())
{
    //redirect them to the index page if logged in
    redirect('/', 'refresh');
}

Also, the auth controller that comes with Ion_auth is really just an example and should be used as a reference. Be sure to read all relevant documentation.

Edit: Do note that using 'refresh' for redirects has its uses but is probably not necessary in this situation. Here's info on that:

Upvotes: 3

Related Questions