nostolgagram/NostolgaGram/api/v1/accounts/register.php
2026-06-24 21:33:55 +01:00

71 lines
No EOL
2.7 KiB
PHP

<?php
/**
* Module B: Manual Registration Validation & Ingestion Engine (Email Explicitly Required)
* Target Path: htdocs/api/v1/accounts/register.php
*/
require_once __DIR__ . '/../../../config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
die(json_encode(["status" => "fail", "message" => "Method not allowed."]));
}
$db = getDB();
// Gather parameter arrays pushed out by the registration wizard forms
$email = trim($_POST['email'] ?? '');
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
$fullName = trim($_POST['first_name'] ?? $_POST['username'] ?? '');
// Enforce safety constraint: Throw error if email or username inputs are missing
if (empty($email) || empty($username) || empty($password)) {
http_response_code(400);
die(json_encode(["status" => "fail", "message" => "An email, username, and password are required to sign up."]));
}
try {
// 1. Conflict Prevention: Make sure username or email choices aren't duplicated in the matrix
$stmt = $db->prepare("SELECT id, username, email FROM users WHERE username = ? OR email = ?");
$stmt->execute([$username, $email]);
$conflict = $stmt->fetch();
if ($conflict) {
http_response_code(400);
$msg = ($conflict['username'] === $username) ? "That username is occupied." : "That email is already registered.";
die(json_encode(["status" => "fail", "message" => $msg]));
}
// 2. Commit account parameters to data storage blocks
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
$cleanFullName = !empty($fullName) ? $fullName : ucfirst($username) . " (NostolgaGram)";
$insertStmt = $db->prepare("INSERT INTO users (username, email, password_hash, full_name) VALUES (?, ?, ?, ?)");
$insertStmt->execute([$username, $email, $hashedPassword, $cleanFullName]);
$stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$db->lastInsertId()]);
$user = $stmt->fetch();
} catch (PDOException $e) {
http_response_code(500);
die(json_encode(["status" => "fail", "message" => "Critical system allocation error during signup processing."]));
}
// 3. Authenticate and dispatch session payload configuration
http_response_code(200);
header("Set-Cookie: sessionid=nostolga_sess_" . $user['id'] . "; Path=/; HttpOnly");
echo json_encode([
"created_user" => [
"pk" => (int)$user['id'],
"username" => $user['username'],
"email" => $user['email'],
"full_name" => $user['full_name'],
"profile_pic_url" => $user['profile_pic'] ?? 'https://picsum.photos/150/150',
"is_private" => false
],
"status" => "ok"
]);
exit;