<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");

require_once "../config.php";

$json = file_get_contents("php://input");
$data = json_decode($json);

$username = $data->username;
$password = $data->password;

$conn = connect_db();

$res = array();

if ($username == "" || $password == "") {
	$res["error"] = "Username and Password are required.";
} else {
	if ($conn->connect_error) {
		die("Connection failed: " . $conn->connect_error);
	} else {
		$sql = "
			SELECT * 
			FROM `user`
			WHERE username = '" . $username . "'
			AND password = '" . $password . "'
			LIMIT 1
		";
		$result = $conn->query($sql);

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$["authenticated"] = "User authenticated";
			}
		} else {
			$res["error"] = "User not found";
		}
	}
}

$conn->close();
echo json_encode($res);
