· 4 years ago · Jan 09, 2021, 11:48 AM
1<?php
2
3namespace App\Controllers;
4
5use \Firebase\JWT\JWT;
6use App\Models\Form_Model;
7use CodeIgniter\RESTful\ResourceController;
8
9class Auth extends ResourceController
10{
11 public function __construct()
12 {
13 $this->auth = new Form_Model();
14 helper('form');
15 $this->form_validation = \Config\Services::validation();
16 }
17
18 public function privateKey()
19 {
20 $privateKey = 'kunciprivatekey';
21 return $privateKey;
22 }
23 public function register()
24 {
25 try {
26 $json = $this->request->getJSON();
27
28 $username = $json->username;
29 $fullname = $json->fullname;
30 $password = $json->password;
31 $passconf = $json->passconf;
32 $email = $json->email;
33
34 $data = [
35 'username' => $username,
36 'fullname' => $fullname,
37 'password' => $password,
38 'passconf' => $passconf,
39 'email' => $email
40 ];
41
42 if ($this->form_validation->run($data, 'register')) {
43 $password_hash = password_hash($password, PASSWORD_BCRYPT);
44
45 $dataInsert = [
46 'username' => $username,
47 'fullname' => $fullname,
48 'password' => $password_hash,
49 'email' => $email
50 ];
51
52 $insert = $this->model->register($dataInsert);
53
54 if ($insert) {
55 $output = [
56 'status' => 200,
57 'message' => 'Berhasil registrasi User'
58 ];
59 return $this->respond($output, 200);
60 } else {
61 $output = [
62 'status' => 400,
63 'message' => 'Gagal egistrasi User'
64 ];
65 return $this->respond($output, 400);
66 }
67 } else {
68 $message = $this->form_validation->getErrors();
69 return $this->respond($message, 400);
70 }
71 } catch (\Throwable $th) {
72 return $this->respond($th, 400);
73 }
74 }
75
76 public function login()
77 {
78 $username = $this->request->getPost('username');
79 $password = $this->request->getPost('password');
80
81 $cek_login = $this->auth->cek_login($username);
82
83 var_dump($cek_login['password']);
84
85 if (password_verify($password, $cek_login['password'])) {
86 $secret_key = $this->privateKey();
87 $issuer_claim = "THE_CLAIM";
88 $audience_claim = "THE_AUDIENCE";
89 $issuedat_claim = time();
90 $notbefore_claim = $issuedat_claim + 10;
91 $expire_claim = $issuedat_claim + 3600;
92 $token = array(
93 "iss" => $issuer_claim,
94 "aud" => $audience_claim,
95 "iat" => $issuedat_claim,
96 "nbf" => $notbefore_claim,
97 "exp" => $expire_claim,
98 "data" => array(
99 "id" => $cek_login['id'],
100 "fullname" => $cek_login['fullname'],
101 "username" => $cek_login['username'],
102 "email" => $cek_login['email']
103 )
104 );
105
106 $token = JWT::encode($token, $secret_key);
107
108 $output = [
109 'status' => 200,
110 'message' => 'Berhasil login',
111 "token" => $token,
112 "username" => $username,
113 ];
114 return $this->respond($output, 200);
115 } else {
116 $output = [
117 'status' => 401,
118 'message' => 'Login failed',
119 "password" => $password
120 ];
121 return $this->respond($output, 401);
122 }
123 }
124
125 public function update($id = NULL)
126 {
127 $data = $this->request->getRawInput();
128 $simpan = $this->auth->updateData($data, $id);
129 if ($simpan) {
130 $msg = ['message' => 'Updated category successfully'];
131 $response = [
132 'status' => 200,
133 'error' => false,
134 'data' => $msg,
135 ];
136 return $this->respond($response, 200);
137 }
138 }
139}
140