Advertisement
Guest User

Untitled

a guest
May 20th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.57 KB | None | 0 0
  1.     public function auth_get() {
  2.         $id_users = $this->get('id_users');
  3.  
  4.         if($id_users === NULL) {
  5.             $auth = $this->distro->getAuth();
  6.         } else {
  7.             $auth = $this->distro->getAuth($id_users);
  8.         }
  9.  
  10.         if($auth) {
  11.             $this->response([
  12.                 "Status"    => true,
  13.                 "Message"   => "Berhasil Mendapatkan Data!",
  14.                 "Data"      => $auth
  15.             ],REST_Controller::HTTP_OK);
  16.         } else {
  17.             $this->response([
  18.                 "Status"    => false,
  19.                 "Message"   => "Data Users Gagal Dapatkan"
  20.             ],REST_Controller::HTTP_BAD_REQUEST);
  21.         }
  22.     }
  23.  
  24.     public function userRegister_post(){
  25.         $user_email = $this->post("email");
  26.         $user_username = $this->post("username");
  27.         $user_password = $this->post("password");
  28.  
  29.         $checkEmail = $this->distro->checkUserByEmail($user_email);
  30.         if(count($checkEmail) == 0){
  31.             $checkUsername = $this->distro->checkUserByUsername($user_username);
  32.             if(count($checkUsername) == 0) {
  33.                 $user_data = [
  34.                     'email' => $user_email,
  35.                     'username' => $user_username,
  36.                     'password' => password_hash(base64_encode($user_password), PASSWORD_DEFAULT),
  37.                 ];
  38.  
  39.                 $createUser = $this->distro->createUser($user_data);
  40.  
  41.                 if($createUser > 0){
  42.                     $this->response([
  43.                         "status" => TRUE,
  44.                         "code" => 200,
  45.                         "message" => "USER CREATED",
  46.                         "data" => $user_data
  47.                     ], REST_Controller::HTTP_CREATED);
  48.                 } else {
  49.                     $this->response([
  50.                         "status" => FALSE,
  51.                         "code" => 400,
  52.                         "message" => "FAILED TO CREATE USER"
  53.                     ], REST_Controller::HTTP_BAD_REQUEST);
  54.                 }
  55.             } else {
  56.                 $this->response([
  57.                     "status" => FALSE,
  58.                     "code" => 400,
  59.                     "message" => "USERNAME HAS TAKEN"
  60.                 ], REST_Controller::HTTP_BAD_REQUEST);
  61.             }
  62.         } else {
  63.             $this->response([
  64.                 "status" => FALSE,
  65.                 "code" => 400,
  66.                 "message" => "USER WITH EMAIL IS EXIST"
  67.             ], REST_Controller::HTTP_BAD_REQUEST);
  68.         }
  69.     }
  70.  
  71.     public function user_delete() {
  72.         $id_users = $this->delete('id_users');
  73.    
  74.         if($id_users === NULL) {
  75.             $this->response([
  76.                 "status"    => false,
  77.                 "message"   => "Silahkan Masukkan Id User!"
  78.             ],REST_Controller::HTTP_BAD_REQUEST);
  79.         } else {
  80.             if($this->distro->deleteUsers($id_users) > 0) {
  81.                 $this->response([
  82.                     "Status"    => true,
  83.                     "Message"   => "Data User Berhasil Dihapus!"
  84.                 ],REST_Controller::HTTP_NO_CONTENT);
  85.             } else {
  86.                 $this->response([
  87.                     "status"    => false,
  88.                     "Message"   => "Gagal Menghapus Data User! Tidak Ada ID Yang Cocok!"
  89.                 ],REST_Controller::HTTP_BAD_REQUEST);
  90.             }
  91.         }
  92.     }
  93.  
  94.  
  95.     public function auth_post(){
  96.         $user_email = $this->post('email');
  97.         $user_password = password_hash(base64_encode($this->post('password')), PASSWORD_DEFAULT);
  98.                          
  99.         $checkEmail = $this->distro->checkUserByEmail($user_email);
  100.         if(count($checkEmail) > 0){
  101.             $user = $checkEmail[0];
  102.             $verify = password_verify(base64_encode($user_password), $user['password']);
  103.             if($verify){
  104.                 $this->response([
  105.                     "status" => TRUE,
  106.                     "code" => 200,
  107.                     "message" => "Login Successfull!",
  108.                     "data" => $user
  109.                 ], REST_Controller::HTTP_OK);
  110.             } else {
  111.                 $this->response([
  112.                     "status" => FALSE,
  113.                     "code" => 400,
  114.                     "message" => "Wrong Email/Password",
  115.                     "password" => $password
  116.                 ], REST_Controller::HTTP_BAD_REQUEST);
  117.             }
  118.         } else {
  119.             $this->response([
  120.                 "status" => FALSE,
  121.                 "code" => 404,
  122.                 "message" => "Account not found!"
  123.             ], REST_Controller::HTTP_NOT_FOUND);
  124.         }
  125.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement