51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Api\Register;
|
|
|
|
use Exception;
|
|
use Khofmann\Api\Api;
|
|
use Khofmann\Input\Input;
|
|
use Khofmann\Response\Response;
|
|
use Khofmann\Models\User\User;
|
|
|
|
class Register extends Api
|
|
{
|
|
public function post(): void
|
|
{
|
|
$username = Input::post("username");
|
|
if (empty($username)) throw new Exception("Missing username", 400);
|
|
$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 {
|
|
Response::json(User::create($username, $email, $password));
|
|
} catch (Exception $err) {
|
|
switch ($err->getMessage()) {
|
|
case "Duplicate":
|
|
throw new Exception("A user with this username or email already exists", 400);
|
|
default:
|
|
throw $err;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function patch(): void
|
|
{
|
|
$code = Input::post("code");
|
|
if (empty($code)) throw new Exception("Missing code", 400);
|
|
|
|
try {
|
|
Response::json(User::confirm($code));
|
|
} catch (Exception $err) {
|
|
switch ($err->getMessage()) {
|
|
case "NotFound":
|
|
throw new Exception("User not found", 404);
|
|
default:
|
|
throw $err;
|
|
}
|
|
}
|
|
}
|
|
}
|