Guest User

Untitled

a guest
Feb 2nd, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. public function login(){
  2. $options = ['cost' => 12,];
  3. $username = $this->input->post('username');
  4. $password = $this->input->post('password');
  5.  
  6. $this->db->where('email',$username);
  7. $user = $this->db->get('app_users');
  8.  
  9. if ($user->num_rows() < 1){
  10. echo $this->jsonify(array('status'=>'failed', 'error' => 'invalid email id'));
  11. exit;
  12. }
  13. else {
  14. $user_data = $user->result()[0];
  15.  
  16. if(hash('SHA256',$password) != $user_data->password){
  17. echo $this->jsonify(array('status'=>'failed', 'error' => 'invalid password'));
  18. exit;
  19. }
  20.  
  21. unset($user_data->password);
  22. $new_login_token = $this->random_text();
  23. $user_data->login_token = $new_login_token;
  24.  
  25. $this->db->where('id',$user_data->id);
  26. $this->db->set('last_activity',date('Y-m-d H:i:s',time()));
  27. $this->db->set('last_activity_ip',$_SERVER['REMOTE_ADDR']);
  28. $this->db->set('login_token',$new_login_token);
  29. $this->db->set('token_issued_on',date('Y-m-d H:i:s',time()));
  30. $this->db->update('app_users');
  31.  
  32. echo $this->jsonify(
  33. array(
  34. 'status'=>'logged in',
  35. 'user' => (array) $user_data
  36. )
  37. );
  38. }
  39.  
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. public function keepalive(){
  50.  
  51. sleep(2);
  52.  
  53. $token = $this->input->post('login_token');
  54.  
  55. if(!$token){
  56. echo $this->jsonify(array('status'=>'not logged in'));
  57. exit;
  58. }
  59.  
  60. $this->db->where('login_token',$token);
  61. $user = $this->db->get('app_users');
  62.  
  63. if ($user->num_rows() < 1){
  64. echo $this->jsonify(array('status'=>'not logged in'));
  65. exit;
  66. }
  67. else {
  68. $user_data = $user->result()[0];
  69.  
  70. $time_diff = (time() - strtotime($user_data->token_issued_on));
  71. if($time_diff > 1296000){
  72. echo $this->jsonify(array('status'=>'not logged in'));
  73. log_message('info', "Token expired for user id {$user_data->id}");
  74. exit;
  75. }
  76.  
  77. // user is valid and good to go.
  78. // update old login token on both client and server ends
  79. // update last activity time, token issued on and last activity ip fields.
  80.  
  81. unset($user_data->password);
  82. $new_login_token = $this->random_text();
  83. $user_data->login_token = $new_login_token;
  84.  
  85. $this->db->where('id',$user_data->id);
  86. $this->db->set('last_activity',date('Y-m-d H:i:s',time()));
  87. $this->db->set('last_activity_ip',$_SERVER['REMOTE_ADDR']);
  88. $this->db->set('login_token',$new_login_token);
  89. $this->db->set('token_issued_on',date('Y-m-d H:i:s',time()));
  90. $this->db->update('app_users');
  91.  
  92. echo $this->jsonify(
  93. array(
  94. 'status'=>'logged in',
  95. 'user' => (array) $user_data
  96. )
  97. );
  98. }
  99. }
Add Comment
Please, Sign In to add comment