Files
tsd_test/www/first.loc/api/authentication-jwt/create_user.php
2026-03-25 13:51:25 +00:00

69 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// Заголовки
header("Access-Control-Allow-Origin:*");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// Подключение к БД
// Файлы, необходимые для подключения к базе данных
include_once __DIR__."/Config/Database.php";
//include_once __DIR__."/Config/DatabasePGSQL.php";
include_once __DIR__."/Objects/User.php";
// Получаем соединение с базой данных
$database = new Database();
$db = $database->getConnection();
// Создание объекта "User"
$user = new User($db);
// Отправляемые данные будут здесь
// Получаем данные
$data = json_decode(file_get_contents("php://input"));
// Устанавливаем значения
$user->firstname = $data->firstname;
$user->lastname = $data->lastname;
$user->email = $data->email;
$user->password = $data->password;
// Поверка на существование e-mail в БД
// $email_exists = $user->emailExists();
// Здесь будет метод create()
// Создание пользователя
if (
!empty($user->firstname) &&
!empty($user->email) &&
// $email_exists == 0 &&
!empty($user->password) &&
$user->create()
) {
// Устанавливаем код ответа
http_response_code(200);
// Покажем сообщение о том, что пользователь был создан
echo json_encode(array("message" => "Пользователь был создан"));
}
// Сообщение, если не удаётся создать пользователя
else {
// Устанавливаем код ответа
http_response_code(400);
// Покажем сообщение о том, что создать пользователя не удалось
echo json_encode(array("message" => "Невозможно создать пользователя"));
}