<?php declare(strict_types=1);
namespace App\Authentication\Controller;
use App\Customer\Service\RegistrationService;
use App\Framework\Controller\APIController;
use App\Framework\Exception\APIException;
use Doctrine\ORM\ORMException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use App\Authentication\Repository\CloudflareTurnstileRepository;
use Psr\Log\LoggerInterface;
#[Route(path: '/api/registration')]
class RegistrationController extends APIController
{
public function __construct(
private LoggerInterface $logger,
private CloudflareTurnstileRepository $cfRepository,
) {}
/**
* @throws ORMException
*
* @return array<string, int|bool>
*/
#[Route(path: '', methods: ['POST'])]
public function registerAction(
AuthorizationCheckerInterface $authChecker,
RegistrationService $registrationService,
): array {
if ($authChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
throw new APIException(400, 'User is already logged in');
}
// Parse the body
$data = $this->getRequestBody();
$firstName = $data['firstName'];
$lastName = $data['lastName'];
$email = $data['email'];
$password = $data['password'];
$cfToken = $data['cfToken'];
// Validate Cloudflare Turnstile
$isTokenValid = $this->cfRepository->validateToken($cfToken);
if (!$isTokenValid) {
$this->logger->info("Token $cfToken is invalid");
throw new APIException(400, 'Invalid CF Turnstile token');
} else {
$this->logger->info("Token $cfToken validated successfully");
}
// Create the user with the data
$registrationService->registerCustomer($firstName, $lastName, $email, $password);
return [
'httpCode' => 201,
'status' => true,
];
}
}