Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2.  
  3. Class Api extends CI_Controller
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->load->model('api_model','api');
  9.  
  10. header("Access-Control-Allow-Origin: *");
  11. header("Access-Control-Allow-Methods: GET, OPTIONS");
  12. header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");
  13. if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
  14. die();
  15. }
  16. }
  17.  
  18. public function login()
  19. {
  20.  
  21. $this->form_validation->set_rules('username', 'Username', 'required');
  22. $this->form_validation->set_rules('password', 'Password', 'required');
  23. if($this->form_validation->run() == FALSE)
  24. {
  25. $output['errors'] = $this->form_validation->error_array();
  26. } else {
  27. $username = $this->input->post('username');
  28. $password = $this->input->post('password');
  29. $userdata = $this->api->authenticate($username, $password);
  30. $id = $userdata['user_id'];
  31. $token = $userdata['token'];
  32. if($id != false) {
  33.  
  34. if(! isset($token)) {
  35. $key = base64_encode('add some randome key here');
  36. $payload = array(
  37. "iss" => "Add website address here",
  38. "iat" => time(),
  39. "exp" => time() + (3600 * 24 * 15),
  40. "context" => [
  41. "user" => [
  42. "username" => $username,
  43. "user_id" => $id
  44. ]
  45. ]
  46. );
  47.  
  48. $jwt = JWT::encode($payload, $key);
  49.  
  50. $this->db->insert('daktari_tokens', array(
  51. 'user_id'=>$id,
  52. 'value'=>$jwt,
  53. 'date_created'=>$payload['iat'],
  54. 'date_expiration'=>$payload['exp']));
  55.  
  56. $output['errors'] = "";
  57. $output['token'] = $jwt;
  58. $output['user_id'] = $id;
  59. } else {
  60. $output['errors'] = "";
  61. $output['token'] = $token;
  62. $output['user_id'] = $id;
  63. }
  64.  
  65. } else {
  66. $output['errors'] = 'User does not exist';
  67. }
  68.  
  69. }
  70.  
  71. echo json_encode($output);
  72. }
  73.  
  74. public function register()
  75. {
  76. $this->form_validation->set_rules('firstname', 'Firstname', 'required');
  77. $this->form_validation->set_rules('lastname', 'Lastname', 'required');
  78. $this->form_validation->set_rules('username','Username','required|is_unique[daktari_users.username]', array(
  79. 'required' => 'You have not provided %s.',
  80. 'is_unique' => 'This %s already exists.'
  81. ));
  82. $this->form_validation->set_rules('password','Password','required|min_length[8]|max_length[64]');
  83. if($this->form_validation->run() == FALSE)
  84. {
  85. $message = $this->form_validation->error_array();
  86. echo json_encode($message);
  87. } else {
  88.  
  89. if($this->api->create_user())
  90. {
  91. $output['errors'] = 'Account successfully created';
  92. } else {
  93. // echo json_encode('Unable to create account');
  94. $output['errors'] = 'Unable to create account';
  95. }
  96. echo json_encode($output);
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement