benedmunds / CodeIgniter-Ion-Auth

Simple and Lightweight Auth System for CodeIgniter
http://benedmunds.com/ion_auth/
MIT License
2.34k stars 1.14k forks source link

Logout function not work after second login #1266

Closed vipinvid closed 5 years ago

vipinvid commented 6 years ago

In my application if i login first time it behave normal but after second time login i can't logout it redirects me to my previous page

vipinvid commented 6 years ago
function login()
{
    if (!$this->ion_auth->logged_in())
    {

        $this->data['title']                    = $this->lang->line('login');
        $this->data['message']                  = "";
        $this->data['req_from']                 = ($this->uri->segment(4)) ? 
        $this->uri->segment(4) : ($this->input->post('req_from'));
        if ($this->input->post()) {
            $this->form_validation->set_rules(
            'identity', 
            $this->lang->line('email') , 
            'required|valid_email|xss_clean');
            $this->form_validation->set_rules(
            'password', 
            $this->lang->line('password') , 
            'required|xss_clean');
            $this->load->library(array('email','form_validation'));
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            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
                    $this->prepare_flashmessage($this->lang->line('success_login'), 0);
                    redirect('auth/index/' . $this->data['req_from']);
                    }
                else {
                        // if the login was un-successful
                        // redirect them back to the login page
                        $this->prepare_flashmessage($this->ion_auth->errors() , 1);
                    redirect('auth/login');
                    }
           }
    }
    $this->data['identity']                 = array(
        'name'                              => 'identity',
        'id'                                => 'identity',
        'type'                              => 'text',
 'placeholder'                      => $this->lang->line($this->lang->line('email')) , 
        'value'                             => $this->form_validation->set_value('identity') ,
    );
    $this->data['password']                 = array(
        'name'                              => 'password',
        'id'                                => 'password',
        'type'                              => 'password',
'placeholder'                       => $this->lang->line($this->lang->line('password')) ,
    );
    $this->data['css']                      = array('form');
    $this->data['title']                    = $this->lang->line('login');
    $this->data['active_class']             = 'login';
    $this->data['content']                  = "site/login";
    $this->_render_page('templates/site_template', $this->data);

}
else
{
    redirect('welcome','refresh');
}

} // log the user out function logout($param = '') {

    $this->data['title']                    = $this->lang->line('logout');
    $this->data['active_class']             = $this->lang->line('logout');
    // log the user out
    $logout = $this->ion_auth->logout();
    // redirect them to the login page
    if ($param == "password_changed") 
        $this->prepare_flashmessage($this->lang->line('password_change_success_login_to_continue') , 0);
    else $this->prepare_flashmessage($this->lang->line('success_logout') , 0);
    redirect('auth/login', 'refresh');

}
benedmunds commented 6 years ago

when you call logout() see what errors() returns

vipinvid commented 6 years ago

No error occurs it back me login

benedmunds commented 6 years ago

You’ll need to troubleshoot this a bit.

Start by commenting out the redirects and view loads. Add var dumps of the logout() call, along with var dumping the output of errors(). If logout() isn’t throwing any errors then the logout call is likely working and there’s a logic issue somewhere.

vipinvid commented 6 years ago

in logout function call $logout = $this->ion_auth->logout(); echo "logout = $logout", exit; /*****/ It display logout=1 but calling login again logged me without authenticating

benedmunds commented 6 years ago

Make sure there is a redirect after logout and before login so CodeIgniter can initialize the session properly. Let me know if that affects your results.

vipinvid commented 6 years ago
function logout($param = '')
{
    $this->data['title']                    = $this->lang->line('logout');

    $this->data['active_class']             = $this->lang->line('logout');

    // log the user out

    $logout = $this->ion_auth->logout();

    // redirect them to the login page
    if ($param == "password_changed") 
        $this->prepare_flashmessage($this->lang->line('password_change_success_login_to_continue') , 0);
    else $this->prepare_flashmessage($this->lang->line('success_logout') , 0);
    redirect('auth/login', 'refresh');
}

///////////////////////////////////////////// above is my logout function

vipinvid commented 6 years ago
function login()
{
    if (!$this->ion_auth->logged_in())
    {
    $this->data['title']                    = $this->lang->line('login');
    $this->data['message']                  = "";
    $this->data['req_from']                 = ($this->uri->segment(4)) ;

    if ($this->input->post()) {
        $this->form_validation->set_rules(
        'identity', 
        $this->lang->line('email') , 
        'required|valid_email|xss_clean');
        $this->form_validation->set_rules(
        'password', 
        $this->lang->line('password') , 
        'required|xss_clean');
        $this->load->library(array('email','form_validation'));
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        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
                $this->prepare_flashmessage($this->lang->line('success_login'), 0);
                redirect('auth/index/' . $this->data['req_from'], 'refresh');
            }
            else {
                // if the login was un-successful
                // redirect them back to the login page
                $this->prepare_flashmessage($this->ion_auth->errors() , 1);
                redirect('auth/login', 'refresh');
            }
        }
    }
    $this->data['identity']                 = array(
        'name'                              => 'identity',
        'id'                                => 'identity',
        'type'                              => 'text',
 'placeholder'                      => $this->lang->line($this->lang->line('email')) , 
        'value'                             => $this->form_validation->set_value('identity') ,
    );
    $this->data['password']                 = array(
        'name'                              => 'password',
        'id'                                => 'password',
        'type'                              => 'password',
'placeholder'                       => $this->lang->line($this->lang->line('password')) ,
    );
    $this->data['css']                      = array('form');
    $this->data['title']                    = $this->lang->line('login');
    $this->data['active_class']             = 'login';
    $this->data['content']                  = "site/login";
    $this->_render_page('templates/site_template', $this->data);

}
else
{
        redirect('welcome','refresh');
}

}

//////////////////////////////////////// and this is login function

benedmunds commented 6 years ago

Interesting, and login() is returning false on login? but only the second time?

No errors on login?

You should add some troubleshooting code in the model login method next.

vipinvid commented 6 years ago

what type of code ??

benedmunds commented 6 years ago

dump/log at various points to determine where and why it's failing. I'd pay particular interested around login attempts logic and CI session logic.

vipinvid commented 6 years ago

on redirecting to other page login function not working

benedmunds commented 5 years ago

@vipinvid any update on this?

benedmunds commented 5 years ago

Closing due to age, will reopen when/if we revisit this.