Register endpoint

This commit is contained in:
2024-07-22 02:21:30 +02:00
parent d94c342bd6
commit af588ab174
9 changed files with 192 additions and 43 deletions
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace Khofmann\GUID;
class GUID
{
public static function v4($data = null)
{
// Generate 16 bytes (128 bits) of random data or use the data passed into the function.
$data = $data ?? random_bytes(16);
assert(strlen($data) == 16);
// Set version to 0100
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set bits 6-7 to 10
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
// Output the 36 character UUID.
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
}
+33
View File
@@ -8,6 +8,8 @@ use DateTime;
use Khofmann\Database\Database;
use Config\Config;
use JsonSerializable;
use Khofmann\GUID\GUID;
use PDOException;
class User implements JsonSerializable
{
@@ -153,6 +155,37 @@ class User implements JsonSerializable
}
}
public static function create(string $username, string $email, string $password)
{
$db = Database::getInstance();
$guid = GUID::v4();
$stmt = $db->prepare(
"INSERT INTO
egb_benutzer(benutzer, passwort, email, confirmationcode)
VALUES(:USR, :PAS, :EMA, :COD)"
);
$stmt->bindValue(":USR", $username);
$stmt->bindValue(":PAS", password_hash($password, PASSWORD_DEFAULT));
$stmt->bindValue(":EMA", $email);
$stmt->bindValue(":COD", $guid);
try {
$stmt->execute();
mail(
$email,
"Account activation GuestBookDB",
"Hello $username. To activate your account, visit https://khofmann.userpage.fu-berlin.de/phpCourse/exam/confirm?c=$guid"
);
return true;
} catch (Exception $err) {
if ($err->getCode() === "23000") throw new Exception("Duplicate");
throw $err;
}
}
/*
* Members
*/
+5
View File
@@ -14,6 +14,11 @@ class Response
public static function json($value, int $options = 0, int $dept = 512)
{
if (is_bool($value)) {
Response::response()->header('Content-Type: application/json; charset=utf-8');
echo json_encode($value, $options, $dept);
exit(0);
}
return SimpleRouter::response()->json($value, $options, $dept);
}
}