CodeIgniter Forums
How to manage Codeigniter session with Ionic 3 application? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: How to manage Codeigniter session with Ionic 3 application? (/thread-71791.html)



How to manage Codeigniter session with Ionic 3 application? - amilaperera - 09-25-2018

 have developed Codeigniter back-end and I want to use same controllers with Ionic 3 application. Ionic 3 login works perfect with back-end controller and creates session and I store user data in the session variable. When I access another controller via Ionic 3 after login success, it has no any user data in the session. I need to keep session data until it destroy on logout because user data is used in each controller access whether user is logged in or not. Here is my code.
Code:
//main login function
function user_login(){

   //Allow Cross origin
   header('Access-Control-Allow-Origin:*');

   $username = $this->input->post('username');
   $password = MD5($this->input->post('password'));

   $data = array('status'=>false,'message'=>'','data'=>'');

   //check a session exists,if not create new
   if(!$this->session->userdata('username')){
       $user_data = array(
               'username' => $username,
               'attempts' => 0,
               'logged_in' => false
       );
       $this->session->set_userdata($user_data);
   }

   $attempts = $this->session->userdata('attempts'); //get number of tried attempts

   $this->session->set_userdata('attempts',($attempts + 1));//increse number of attempts from one



   {
       $lognError = $this->login_model->get_data_from_login_error_table($username);
       $b = true;

       if(sizeof($lognError) > 0){

           $trialsCount = $lognError['trials_count']; //get number of attempts that tried

           if($trialsCount >= 25){

               $b = false;
           }
       }

       if($b == true){

           $is_login = $this->login_model->login($username,$password);

           if($is_login != null) {

               $this->session->set_userdata('loged_in', true);
               $this->session->set_userdata($is_login);
               $this->session->set_userdata('user_details', $user_details);
           } else {
               $data['status'] = false;
               $data['message'] = 'Incorrect username or password';
           }
       }

   }
   // }
   echo json_encode($data);
}

//Checking whether user logged or not
Code:
public function is_login(){

   $details = $this->session->userdata('username');
   $is_loged_in = $this->session->userdata('loged_in');

   if(!$details || !$is_loged_in) {
       redirect('login');
   }
}

$this->session->userdata(‘username’); returns null when I access via Ionic 3 but works fine with browser. How can I use PHP backend with ionic 3?
Help me out to solve this problem. Thanks in advanced.


RE: How to manage Codeigniter session with Ionic 3 application? - dave friend - 09-25-2018

What you describe is usually due to improper session and/or cookie config settings. Show the associated $config values and maybe someone will spot a problem.


RE: How to manage Codeigniter session with Ionic 3 application? - ignitedcms - 09-25-2018

Things to check, how is it loading pages, looks all ajax one page, check cors, are you acquiring js from a cdnjs site, if so this might be causing issues.