Confirm Register

This commit is contained in:
Kilian Hofmann 2024-07-22 13:49:33 +02:00
parent 06ce614c2b
commit 2fde820942
5 changed files with 115 additions and 5 deletions

View File

@ -30,4 +30,21 @@ class Register extends Api
}
}
}
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;
}
}
}
}

View File

@ -119,6 +119,45 @@ paths:
}
tags:
- Register
patch:
summary: Confirm register
description: Confirm a registration
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/ConfirmRequest"
responses:
200:
description: Success
content:
application/json:
schema:
$ref: "#/components/schemas/BooleanResponse"
examples:
Success:
value: true
400:
description: Missing fields
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
examples:
Missing fields:
value: { "message": "Missing code" }
404:
description: User not found
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
examples:
User not found:
value: { "message": "User not found" }
tags:
- Register
/user{id}:
get:
summary: Get user
@ -312,11 +351,20 @@ components:
type: string
password:
type: string
ConfirmRequest:
type: object
required:
- code
properties:
code:
type: string
format: uuid4
securitySchemes:
BasicAuth:
type: apiKey
name: token
in: header
format: uuid4
tags:
- name: Login/Logout
- name: Register

File diff suppressed because one or more lines are too long

View File

@ -34,6 +34,28 @@ class User implements JsonSerializable
$this->postCount = $postCount;
}
private static function getByConfirmCode(string $code): User
{
$db = Database::getInstance();
$stmt = $db->prepare(
"SELECT
b.id, b.benutzer, b.status, b.email, b.image, b.isadmin, b.zeitstempel,
(SELECT COUNT(*) FROM egb_gaestebuch WHERE benutzer_id = b.id) as postCount
FROM
egb_benutzer AS b
WHERE
confirmationcode = :COD"
);
$stmt->bindValue(":COD", $code);
$stmt->execute();
$data = $stmt->fetch();
if (!$data) throw new Exception("NotFound");
return new User($data["id"], $data["benutzer"], $data["status"], $data["email"], $data["zeitstempel"], $data["image"], $data["isadmin"] === 1, $data["postCount"]);
}
/*
* Statics
*/
@ -189,7 +211,21 @@ class User implements JsonSerializable
public static function confirm(string $confirmCode): bool
{
$db = Database::getInstance();
$user = User::getByConfirmCode($confirmCode);
$stmt = $db->prepare(
"UPDATE
egb_benutzer
SET
status = 1,
confirmationcode = NULL
WHERE id = :UID"
);
$stmt->bindValue(":UID", $user->getID());
return $stmt->execute();
}
/*
* Members
*/

View File

@ -19,6 +19,7 @@ SimpleRouter::all("/", function () {
// Login
SimpleRouter::post("/login", [Api\Login\Login::class, "post"]);
SimpleRouter::post("/register", [Api\Register\Register::class, "post"]);
SimpleRouter::patch("/register", [Api\Register\Register::class, "patch"]);
/*
* Normal Auth routes
*/