2025-06-26 14:09:25 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
use CodeIgniter\Controller;
|
|
|
|
|
use \Firebase\JWT\JWT;
|
2025-09-03 15:36:55 +07:00
|
|
|
use CodeIgniter\Cookie\Cookie;
|
2025-06-26 14:09:25 +07:00
|
|
|
|
|
|
|
|
class Auth extends Controller {
|
|
|
|
|
use ResponseTrait;
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 15:36:55 +07:00
|
|
|
// public function login() {
|
|
|
|
|
// $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->getRowArray();
|
|
|
|
|
|
|
|
|
|
// if (!$row) {
|
|
|
|
|
// return $this->fail('User not found.', 401); // Use 401 for authentication failures
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (!password_verify($password, $row['password'])) {
|
|
|
|
|
// return $this->fail('Invalid password.', 401);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // JWT payload
|
|
|
|
|
// $payload = [
|
|
|
|
|
// 'userid' => $row['id'],
|
|
|
|
|
// 'username' => $row['username'],
|
|
|
|
|
// 'exp' => time() + 3600
|
|
|
|
|
// ];
|
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// $jwt = JWT::encode($payload, $key, 'HS256');
|
|
|
|
|
// } catch (Exception $e) {
|
|
|
|
|
// return $this->fail('Error generating JWT: ' . $e->getMessage(), 500);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Update last_login
|
|
|
|
|
// //$this->userModel->update($user['id'], ['lastlogin' => date('Y-m-d H:i:s')]);
|
|
|
|
|
|
|
|
|
|
// $response = [
|
|
|
|
|
// 'status' => 'success',
|
|
|
|
|
// 'message' => 'Login successful',
|
|
|
|
|
// 'token' => $jwt,
|
|
|
|
|
// ];
|
|
|
|
|
// return $this->respond($response);
|
|
|
|
|
// }
|
2025-06-26 14:09:25 +07:00
|
|
|
public function login() {
|
2025-09-03 15:36:55 +07:00
|
|
|
|
2025-06-26 14:09:25 +07:00
|
|
|
$username = $this->request->getVar('username');
|
|
|
|
|
$password = $this->request->getVar('password');
|
|
|
|
|
$key = getenv('JWT_SECRET');
|
|
|
|
|
|
|
|
|
|
if (!$username) {
|
|
|
|
|
return $this->fail('Username required.', 400);
|
|
|
|
|
}
|
2025-09-03 15:36:55 +07:00
|
|
|
|
|
|
|
|
$sql = "SELECT * FROM users WHERE username=" . $this->db->escape($username);
|
2025-06-26 14:09:25 +07:00
|
|
|
$query = $this->db->query($sql);
|
|
|
|
|
$row = $query->getRowArray();
|
|
|
|
|
|
|
|
|
|
if (!$row) {
|
2025-09-03 15:36:55 +07:00
|
|
|
return $this->fail('User not found.', 401);
|
2025-06-26 14:09:25 +07:00
|
|
|
}
|
2025-09-03 15:36:55 +07:00
|
|
|
|
2025-06-26 14:09:25 +07:00
|
|
|
if (!password_verify($password, $row['password'])) {
|
|
|
|
|
return $this->fail('Invalid password.', 401);
|
|
|
|
|
}
|
2025-09-03 15:36:55 +07:00
|
|
|
|
2025-06-26 14:09:25 +07:00
|
|
|
// JWT payload
|
|
|
|
|
$payload = [
|
2025-09-03 15:36:55 +07:00
|
|
|
'userid' => $row['id'],
|
2025-06-26 14:09:25 +07:00
|
|
|
'username' => $row['username'],
|
2025-09-03 15:36:55 +07:00
|
|
|
'exp' => time() + 3600
|
2025-06-26 14:09:25 +07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$jwt = JWT::encode($payload, $key, 'HS256');
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
return $this->fail('Error generating JWT: ' . $e->getMessage(), 500);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 15:36:55 +07:00
|
|
|
// Set cookie (HttpOnly + Secure + SameSite=Strict)
|
|
|
|
|
$this->response->setCookie([
|
|
|
|
|
'name' => 'token',
|
|
|
|
|
'value' => $jwt,
|
|
|
|
|
'expire' => 3600, // 1 jam
|
|
|
|
|
'path' => '/',
|
|
|
|
|
'secure' => true, // set true kalau sudah HTTPS
|
|
|
|
|
'httponly' => true,
|
|
|
|
|
'samesite' => Cookie::SAMESITE_NONE // set true kalau sudah HTTPS
|
|
|
|
|
// 'samesite' => Cookie::SAMESITE_STRICT
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Response tanpa token di body
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Login successful'
|
|
|
|
|
]);
|
2025-06-26 14:09:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function change_pass() {
|
|
|
|
|
$db = \Config\Database::connect();
|
|
|
|
|
$username = $this->request->getJsonVar('username');
|
|
|
|
|
$password = $this->request->getJsonVar('password');
|
|
|
|
|
$password = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
|
|
|
|
|
|
$master = $this->request->getJsonVar('master');
|
|
|
|
|
$masterkey = getenv('masterkey');
|
|
|
|
|
|
|
|
|
|
if($master != $masterkey) {
|
|
|
|
|
return $this->fail('Invalid master key.', 401);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sql = "update users set password='$password' where username='$username'";
|
|
|
|
|
$query = $db->query($sql);
|
|
|
|
|
$response = [
|
|
|
|
|
'message' => "Password Changed for $username"
|
|
|
|
|
];
|
|
|
|
|
return $this->respond($response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function register() {
|
2025-09-03 15:36:55 +07:00
|
|
|
|
2025-06-26 14:09:25 +07:00
|
|
|
$username = $this->request->getJsonVar('username');
|
|
|
|
|
$password = $this->request->getJsonVar('password');
|
|
|
|
|
$password = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
|
|
2025-09-03 15:36:55 +07:00
|
|
|
// $master = $this->request->getJsonVar('master');
|
|
|
|
|
// $masterkey = getenv('MASTERKEY');
|
|
|
|
|
|
|
|
|
|
// if($master != $masterkey) {
|
|
|
|
|
// return $this->fail('Invalid master key.', 401);
|
|
|
|
|
// }
|
2025-06-26 14:09:25 +07:00
|
|
|
|
2025-09-03 15:36:55 +07:00
|
|
|
$sql = "INSERT INTO users(username, password) values('$username', '$password')";
|
2025-06-26 14:09:25 +07:00
|
|
|
$this->db->query($sql);
|
|
|
|
|
$response = [
|
2025-09-03 15:36:55 +07:00
|
|
|
'message' => "User $username created"
|
2025-06-26 14:09:25 +07:00
|
|
|
];
|
|
|
|
|
return $this->respondCreated($response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function checkAuth() {
|
2025-09-03 15:36:55 +07:00
|
|
|
$token = $this->request->getCookie('token');
|
|
|
|
|
$key = getenv('JWT_SECRET');
|
2025-06-26 14:09:25 +07:00
|
|
|
|
2025-09-03 15:36:55 +07:00
|
|
|
if (!$token) {
|
|
|
|
|
return $this->fail('No token found', 401);
|
2025-06-26 14:09:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2025-09-03 15:36:55 +07:00
|
|
|
$decoded = JWT::decode($token, new Key($key, 'HS256'));
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Authenticated',
|
|
|
|
|
'data' => $decoded
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->fail('Invalid or expired token: ' . $e->getMessage(), 401);
|
2025-06-26 14:09:25 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 15:36:55 +07:00
|
|
|
public function logout() {
|
|
|
|
|
return $this->response
|
|
|
|
|
->deleteCookie('token')
|
|
|
|
|
->setJSON(['message' => 'Logout successful']);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 14:09:25 +07:00
|
|
|
}
|