PHP-Course/exam/api/Login/Login.php
2024-07-22 02:21:30 +02:00

36 lines
869 B
PHP

<?php
namespace Api\Login;
use Exception;
use Khofmann\Api\Api;
use Khofmann\Input\Input;
use Khofmann\Response\Response;
use Khofmann\Models\User\User;
class Login extends Api
{
public function post()
{
$email = Input::post("email");
if (empty($email)) throw new Exception("Missing email", 400);
$password = Input::post("password");
if (empty($password)) throw new Exception("Missing password", 400);
try {
return Response::json(User::logIn($email, $password));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "Failed":
throw new Exception("Login failed", 500);
case "NotFound":
throw new Exception("User not found", 404);
case "Invalid":
throw new Exception("Invalid username or password", 401);
default:
throw $err;
}
}
}
}