'Unauthorized', 'message' => 'Invalid API Key.'], 401); } } checkApiKey(); $pdo = db(); $method = $_SERVER['REQUEST_METHOD']; switch ($method) { case 'POST': $data = json_decode(file_get_contents('php://input'), true); if (!$data || empty($data['email']) || empty($data['password'])) { sendJsonResponse(['error' => 'Bad Request', 'message' => 'Email and password are required.'], 400); } $email = $data['email']; $password = $data['password']; try { $stmt = $pdo->prepare("SELECT id, name, email, password FROM users WHERE email = :email"); $stmt->execute(['email' => $email]); $user = $stmt->fetch(); if ($user && password_verify($password, $user['password'])) { // For a real API, generate and return a token (e.g., JWT) // For this example, we'll just return a success message and user info (without password) unset($user['password']); sendJsonResponse(['message' => 'Login successful.', 'user' => $user]); } else { sendJsonResponse(['error' => 'Unauthorized', 'message' => 'Invalid credentials.'], 401); } } catch (PDOException $e) { sendJsonResponse(['error' => 'Database Error', 'message' => $e->getMessage()], 500); } break; default: sendJsonResponse(['error' => 'Method Not Allowed', 'message' => '' . $method . ' method is not supported.'], 405); break; }