php - Codeigniter: Ajax Login Redirect issue with Ion Auth -
i trying use ion auth framework codeigniter application.
when user clicks link request goes controller method , checks if user logged in or not. if not redirects auth\login
method. html of login page in success(msg):alert(msg);
ajax request
$('img').click(function() { $.ajax({ url: "<?php echo site_url('gallery/openproductdetail');?>", type: 'post', success: function(msg) { alert(msg); } }); });
controller
function openproductdetail() { if (!$this->ion_auth->logged_in()) { redirect('auth/login'); } else { $this->load->view('modal/productdetail'); } }
login page login.pho original ion auth
<h1><?php echo lang('login_heading');?></h1> <p><?php echo lang('login_subheading');?></p> <div id="infomessage"><?php echo $message;?></div> <?php echo form_open("auth/login");?> <p> <?php echo lang('login_identity_label', 'identity');?> <?php echo form_input($identity);?> </p> <p> <?php echo lang('login_password_label', 'password');?> <?php echo form_input($password);?> </p> <p> <?php echo lang('login_remember_label', 'remember');?> <?php echo form_checkbox('remember', '1', false, 'id="remember"');?> </p> <p><?php echo form_submit('submit', lang('login_submit_btn'));?></p> <?php echo form_close();?> <p><a href="forgot_password"><?php echo lang('login_forgot_password');?></a></p>
$ret; // declare variable return. if (!$this->ion_auth->logged_in()){ if($this->input->is_ajax_request()){ /* ajax request, send url redirected to. */ $ret['url'] = site_url().'auth/login'; $ret['html'] = false; }else{ /* not ajax, standard form submission, let codeigniter handle it*/ redirect('auth/login'); } } else { if($this->input->is_ajax_request()){ $ret['url'] = false; //they logged in, , ajax, don't redirect. $ret['html'] = $this->load->view('modal/productdetail', '', true); //allow html returned server }else{ /* logged in, not ajax, redirect them. */ redirect('modal/productdetail'); } } echo json_encode($ret); //json_encode our $ret array, , send browser
with above, we're creating variable called $ret
holds reference data when performing ajax requests. otherwise, when not using ajax (no javascript), standard form redirection behavior ensues.
now, in our success function, we're going check existence of data.url
, data.html
properties , act accordingly.
datatype: 'json', //add success: knows how parse returned data success:function(data){ if(data.url){ window.location = data.url; }else if(data.html){ $('element').html(data.html); // "element" html element on page wish override partial view } }
Comments
Post a Comment