src/Authentication/Controller/RegistrationController.php line 28

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Authentication\Controller;
  3. use App\Customer\Service\RegistrationService;
  4. use App\Framework\Controller\APIController;
  5. use App\Framework\Exception\APIException;
  6. use Doctrine\ORM\ORMException;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. use App\Authentication\Repository\CloudflareTurnstileRepository;
  10. use Psr\Log\LoggerInterface;
  11. #[Route(path'/api/registration')]
  12. class RegistrationController extends APIController
  13. {
  14.     public function __construct(
  15.         private LoggerInterface $logger,
  16.         private CloudflareTurnstileRepository $cfRepository,
  17.     ) {}
  18.     /**
  19.      * @throws ORMException
  20.      *
  21.      * @return array<string, int|bool>
  22.      */
  23.     #[Route(path''methods: ['POST'])]
  24.     public function registerAction(
  25.         AuthorizationCheckerInterface $authChecker,
  26.         RegistrationService $registrationService,
  27.     ): array {
  28.         if ($authChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  29.             throw new APIException(400'User is already logged in');
  30.         }
  31.         // Parse the body
  32.         $data $this->getRequestBody();
  33.         $firstName $data['firstName'];
  34.         $lastName $data['lastName'];
  35.         $email $data['email'];
  36.         $password $data['password'];
  37.         $cfToken $data['cfToken'];
  38.         // Validate Cloudflare Turnstile
  39.         $isTokenValid $this->cfRepository->validateToken($cfToken);
  40.         if (!$isTokenValid) {
  41.             $this->logger->info("Token $cfToken is invalid");
  42.             throw new APIException(400'Invalid CF Turnstile token');
  43.         } else {
  44.             $this->logger->info("Token $cfToken validated successfully");
  45.         }
  46.         // Create the user with the data
  47.         $registrationService->registerCustomer($firstName$lastName$email$password);
  48.         return [
  49.             'httpCode' => 201,
  50.             'status' => true,
  51.         ];
  52.     }
  53. }