Files
casadoc/public/userportal/api/register.php
T
2026-07-27 21:04:07 +03:00

64 lines
2.1 KiB
PHP

<?php
require __DIR__ . '/_bootstrap.php';
require __DIR__ . '/_codes.php';
require __DIR__ . '/_mail.php';
/**
* @OA\Post(
* path="/register.php",
* tags={"Auth"},
* summary="Register an account and send a verification code",
* description="No token is issued: the e-mail must be confirmed via email-verify.php first.",
* security={},
* @OA\RequestBody(required=true, @OA\JsonContent(
* required={"email","password"},
* @OA\Property(property="email", type="string", format="email"),
* @OA\Property(property="password", type="string", format="password", minLength=8),
* @OA\Property(property="first_name", type="string"),
* @OA\Property(property="last_name", type="string")
* )),
* @OA\Response(response=201, ref="#/components/responses/Success"),
* @OA\Response(response=422, ref="#/components/responses/ValidationError")
* )
*/
require_method('POST');
$in = body();
$email = trim((string) ($in['email'] ?? ''));
$password = (string) ($in['password'] ?? '');
$fields = [];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$fields['email'] = ['Valid email required'];
}
if (strlen($password) < 8) {
$fields['password'] = ['At least 8 characters'];
}
if ($fields) {
json_error(422, 'Validation failed', $fields);
}
$exists = $pdo->prepare('SELECT 1 FROM auth_users WHERE email = ? LIMIT 1');
$exists->execute([$email]);
if ($exists->fetchColumn()) {
json_error(422, 'Validation failed', ['email' => ['Already registered']]);
}
// role_id 2 is "User"; 1 is "Admin" and must never be handed out here.
$pdo->prepare(
"INSERT INTO auth_users (email, password, first_name, last_name, role_id, status, email_verified_at, created_at, updated_at)
VALUES (?, ?, ?, ?, 2, 'Active', NULL, NOW(), NOW())"
)->execute([
$email,
password_hash($password, PASSWORD_DEFAULT),
trim((string) ($in['first_name'] ?? '')) ?: null,
trim((string) ($in['last_name'] ?? '')) ?: null,
]);
if ($code = issue_code($pdo, 'auth_email_verifications', $email)) {
send_code_mail($email, $code);
}
http_response_code(201);
echo json_encode(['success' => true]);