Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function login(){
- $options = ['cost' => 12,];
- $username = $this->input->post('username');
- $password = $this->input->post('password');
- $this->db->where('email',$username);
- $user = $this->db->get('app_users');
- if ($user->num_rows() < 1){
- echo $this->jsonify(array('status'=>'failed', 'error' => 'invalid email id'));
- exit;
- }
- else {
- $user_data = $user->result()[0];
- if(hash('SHA256',$password) != $user_data->password){
- echo $this->jsonify(array('status'=>'failed', 'error' => 'invalid password'));
- exit;
- }
- unset($user_data->password);
- $new_login_token = $this->random_text();
- $user_data->login_token = $new_login_token;
- $this->db->where('id',$user_data->id);
- $this->db->set('last_activity',date('Y-m-d H:i:s',time()));
- $this->db->set('last_activity_ip',$_SERVER['REMOTE_ADDR']);
- $this->db->set('login_token',$new_login_token);
- $this->db->set('token_issued_on',date('Y-m-d H:i:s',time()));
- $this->db->update('app_users');
- echo $this->jsonify(
- array(
- 'status'=>'logged in',
- 'user' => (array) $user_data
- )
- );
- }
- }
- public function keepalive(){
- sleep(2);
- $token = $this->input->post('login_token');
- if(!$token){
- echo $this->jsonify(array('status'=>'not logged in'));
- exit;
- }
- $this->db->where('login_token',$token);
- $user = $this->db->get('app_users');
- if ($user->num_rows() < 1){
- echo $this->jsonify(array('status'=>'not logged in'));
- exit;
- }
- else {
- $user_data = $user->result()[0];
- $time_diff = (time() - strtotime($user_data->token_issued_on));
- if($time_diff > 1296000){
- echo $this->jsonify(array('status'=>'not logged in'));
- log_message('info', "Token expired for user id {$user_data->id}");
- exit;
- }
- // user is valid and good to go.
- // update old login token on both client and server ends
- // update last activity time, token issued on and last activity ip fields.
- unset($user_data->password);
- $new_login_token = $this->random_text();
- $user_data->login_token = $new_login_token;
- $this->db->where('id',$user_data->id);
- $this->db->set('last_activity',date('Y-m-d H:i:s',time()));
- $this->db->set('last_activity_ip',$_SERVER['REMOTE_ADDR']);
- $this->db->set('login_token',$new_login_token);
- $this->db->set('token_issued_on',date('Y-m-d H:i:s',time()));
- $this->db->update('app_users');
- echo $this->jsonify(
- array(
- 'status'=>'logged in',
- 'user' => (array) $user_data
- )
- );
- }
- }
Add Comment
Please, Sign In to add comment