diff --git a/app/Controllers/Auth.php b/app/Controllers/Auth.php index 3affca6..17a63a8 100644 --- a/app/Controllers/Auth.php +++ b/app/Controllers/Auth.php @@ -74,7 +74,71 @@ class Auth extends Controller { } // ok - public function login() { + // public function login() { + + // // Ambil dari JSON Form dan Key .env + // $username = $this->request->getVar('username'); + // $password = $this->request->getVar('password'); + // $key = getenv('JWT_SECRET'); + + // if (!$username) { + // return $this->fail('Username required.', 400); + // } + + // $sql = "SELECT * FROM users WHERE username=" . $this->db->escape($username); + // $query = $this->db->query($sql); + // $row = $query->getResultArray(); + + // if (!$row) { return $this->fail('User not found.', 401); } + // $row = $row[0]; + // if (!password_verify($password, $row['password'])) { + // return $this->fail('Invalid password.', 401); + // } + + // // Buat JWT payload + // $exp = time() + 864000; + // $payload = [ + // 'userid' => $row['id'], + // 'roleid' => $row['role_id'], + // 'username' => $row['username'], + // 'exp' => $exp + // ]; + + // try { + // // Melakukan Hash terhadap Payload dengan Kunci .env menggunakan Algortima HMAC + SHA-256 + // $jwt = JWT::encode($payload, $key, 'HS256'); + // } catch (Exception $e) { + // return $this->fail('Error generating JWT: ' . $e->getMessage(), 500); + // } + + // // Kirim Respon ke HttpOnly yg akan disimpan di browser dan tidak akan dapat diakses oleh siapapun + // // $isSecure = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'; + // $this->response->setCookie([ + // // 'name' => 'token', // nama token + // // 'value' => $jwt, // value dari jwt yg sudah di hash + // // 'expire' => 864000, // 10 hari + // // 'path' => '/', // valid untuk semua path + // // 'secure' => $isSecure, // true for HTTPS, false for HTTP (localhost) + // // 'httponly' => true, // dipakai agar cookie berikut tidak dapat diakses oleh javascript + // // 'samesite' => $isSecure ? Cookie::SAMESITE_NONE : Cookie::SAMESITE_LAX + // 'name' => 'token', + // 'value' => '', + // 'expire' => time() - 3600, + // 'path' => '/', + // 'secure' => true, + // 'httponly' => true, + // 'samesite' => Cookie::SAMESITE_NONE + // ]); + + + // // Response tanpa token di body + // return $this->respond([ + // 'status' => 'success', + // 'code' => 200, + // 'message' => 'Login successful' + // ], 200); + // } + public function login() { // Ambil dari JSON Form dan Key .env $username = $this->request->getVar('username'); @@ -112,24 +176,15 @@ class Auth extends Controller { } // Kirim Respon ke HttpOnly yg akan disimpan di browser dan tidak akan dapat diakses oleh siapapun - // $isSecure = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'; $this->response->setCookie([ - // 'name' => 'token', // nama token - // 'value' => $jwt, // value dari jwt yg sudah di hash - // 'expire' => 864000, // 10 hari - // 'path' => '/', // valid untuk semua path - // 'secure' => $isSecure, // true for HTTPS, false for HTTP (localhost) - // 'httponly' => true, // dipakai agar cookie berikut tidak dapat diakses oleh javascript - // 'samesite' => $isSecure ? Cookie::SAMESITE_NONE : Cookie::SAMESITE_LAX - 'name' => 'token', - 'value' => '', - 'expire' => time() - 3600, - 'path' => '/', - 'secure' => true, - 'httponly' => true, - 'samesite' => Cookie::SAMESITE_NONE + 'name' => 'token', // nama token + 'value' => $jwt, // value dari jwt yg sudah di hash + 'expire' => 864000, // 10 hari + 'path' => '/', // valid untuk semua path + 'secure' => true, // set true kalau sudah HTTPS + 'httponly' => true, // dipakai agar cookie berikut tidak dapat diakses oleh javascript + 'samesite' => Cookie::SAMESITE_NONE ]); - // Response tanpa token di body return $this->respond([