Redirect to original page after logging in (Codeigniter)
Asked Answered
D

1

2

The entire website requires a user to login before viewing the page. If the user is not logged in, he gets redirected to the login/registration page.

Problem: When a user that is not logged in types in a url like http://www.domain.com/listings/1234 , he will be shown that page, but with a darkened page that prevents the user from interacting with the page (jQuery stuff, not a problem here), and a popup modal box with a link to the (Tank Auth) login page http://www.domain.com/login.

After logging in from the login page, the user will be redirected back to the usual page one gets after logging in, ie: http://www.domain.com but that page is passed the variable 1234.

In short: I want the user that has not logged in and has entered the website at http://www.domain.com/listings/1234 to NOT be redirected to http://www.domain.com/login yet, but instead remain on http://www.domain.com/listings/1234 and be shown a modal box with a link to the login page http://www.domain.com/login where if he logins in, he will be redirected back to http://www.domain.com/ instead of the usual page he gets after login and be passed the variable 1234.

How can this be done using Codeigniter?

This is what I have now:

Listings controller

function index(listing_id) {
    $this->load->module('auth');
    if(!$this->auth->tank_auth->is_logged_in()) {
         redirect('login');
    } 

    // Some calls to database etc here...

    $this->load->view('listings', $data);

}
Driest answered 24/12, 2011 at 16:9 Comment(0)
N
2

There are several ways that your question can be accomplished. Hopefully this solution would give some ideas on how to accomplish this.

The solution below uses jquery ui dialog to display the modal and jquery .ajax to handle the login form submission.

Conditionally put this code at the bottom of you page before the </body> tag if the user is not logged in.

<?php if($this->auth->tank_auth->is_logged_in()){ ?>
    <div id="dialog-wrap" class="modal" >
    <?php echo $this->load->view('login_form'); ?>
    </div>
    <script>
        $('#dialog-wrap').modal({
            modal: true,
            autoOpen: true,
        });

        $('#login-form').submit(function(){
            //use .ajax to submit login form and check credentials.
            $.ajax({
                type: 'POST',
                url: <?php echo base_url(); ?>"login.php",
                data: "username="+$('username').val()+'&password='+$('password').val(),
                dataType: 'json',
                success: function(returnData){.
                    if(returnData.status){
                        //close modal box
                    }else {
                        //display message that login failed.
                    }
                }
            });

        });
    </script>
?>

Your ajax login controller

function login(){

  //Use tank auth to validate login information.

  if(authentication is successful) {
    $return_data = array(
        'status'    => true,
        'msg'           => 'Login Successful'
    );
  }else {
        $return_data = array(
        'status'    => false,
        'msg'           => 'Login failed'
    );
  } 

  //Use echo instead of return when you are sending data back to an jquery ajax function.
  echo json_encode($data);
}

If you do it this way then you won't have to rely on codeigniter redirects everything will be handled through ajax.

I hope this gives you an idea of how to handle this.

Nadaba answered 24/12, 2011 at 16:22 Comment(1)
I think this code will do the same as the code snippet I've shown in the original post? Updated the original post with more specific details. Thanks!Driest

© 2022 - 2024 — McMap. All rights reserved.