src/Http/Controller/Auth/AppleController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Http\Controller\Auth;
  3. use App\Application\CommandBus\Command\Apple\SetOrUpdateAppleSignInSubCommand;
  4. use App\Application\CommandBus\Command\User\RegistrationCommand;
  5. use App\Database\Domain\Entity\Log;
  6. use App\Database\Domain\Entity\User\UserProfile;
  7. use App\Database\Domain\Repository\UserRepository;
  8. use App\Infrastructure\Logs\DbLogger;
  9. use App\Infrastructure\Messenger\CommandBus\CommandBusInterface;
  10. use App\Infrastructure\Security\AuthManager;
  11. use App\Infrastructure\Service\AppleHelper;
  12. use App\Infrastructure\Util\Identifier;
  13. use AppleSignIn\ASDecoder;
  14. use AppleSignIn\ASPayload;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @Route("/apple", name="site_apple_")
  21.  */
  22. class AppleController extends AbstractController
  23. {
  24.     /**
  25.      * @Route("/login", name="login")
  26.      */
  27.     public function __invoke(Request $requestAppleHelper $appleHelper): Response
  28.     {
  29.         if ($request->getSession()->get('redirect-uri') == 'ios-login') {
  30.             return $this->redirect($appleHelper->generateLoginLink().'-ios');
  31.         }
  32.         return $this->redirect($appleHelper->generateLoginLink());
  33.     }
  34.     /**
  35.      * @Route("/sign-in", name="sign_in")
  36.      */
  37.     public function register(
  38.         Request $request,
  39.         UserRepository $userRepository,
  40.         AuthManager $authManager,
  41.         CommandBusInterface $commandBus,
  42.         AppleHelper $appleHelper,
  43.         DbLogger $logger
  44.     ): Response {
  45.         try {
  46.             $authCode $request->request->get('code');
  47.             /** @var ASPayload $payload */
  48.             $payload ASDecoder::getAppleSignInPayload($appleHelper->getIdentityToken($authCode));
  49.             $email $payload->getEmail();
  50.             if (null !== $email) {
  51.                 $user $userRepository->findByEmail($email);
  52.                 if (null !== $user) {
  53.                     $commandBus->execute(new SetOrUpdateAppleSignInSubCommand($payload->sub$user->getId()));
  54.                 } else {
  55.                     $command = new RegistrationCommand();
  56.                     $command->id Identifier::generate();
  57.                     $command->name 'Toast Lover';
  58.                     $command->surname '';
  59.                     $command->email $email;
  60.                     $command->username $email;
  61.                     $command->emailConfirmed true;
  62.                     $command->registrationType UserProfile::REGISTRATION_TYPE_APPLE;
  63.                     $command->appleSignInSub $payload->sub;
  64.                     $commandBus->execute($command);
  65.                     $user $userRepository->find($command->id);
  66.                     return $authManager->manualLogin($user'registration_finish_registration');
  67.                 }
  68.             } else {
  69.                 $user $userRepository->findByAppleSignInSub($payload->sub);
  70.                 if (null === $user) {
  71.                     throw new \RuntimeException('User was not found by apple sign in');
  72.                 }
  73.             }
  74.         } catch (\Exception $e) {
  75.             $logger->error($e->getMessage(), Log::SOURCE_APPLE, ['trace' => $e->getTraceAsString()]);
  76.             throw new \RuntimeException('Failed to auth with apple');
  77.         }
  78.         return $authManager->manualLogin($user);
  79.     }
  80.     /**
  81.      * @Route("/sign-in-ios", name="sign_in_ios")
  82.      */
  83.     public function registerIos(
  84.         Request $request,
  85.         UserRepository $userRepository,
  86.         AuthManager $authManager,
  87.         CommandBusInterface $commandBus,
  88.         AppleHelper $appleHelper,
  89.         DbLogger $logger
  90.     ): Response {
  91.         try {
  92.             $authCode $request->request->get('code');
  93.             /** @var ASPayload $payload */
  94.             $payload ASDecoder::getAppleSignInPayload($appleHelper->getIdentityToken($authCode));
  95.             $email $payload->getEmail();
  96.             if (null !== $email) {
  97.                 $user $userRepository->findByEmail($email);
  98.                 if (null !== $user) {
  99.                     $commandBus->execute(new SetOrUpdateAppleSignInSubCommand($payload->sub$user->getId()));
  100.                 } else {
  101.                     $command = new RegistrationCommand();
  102.                     $command->id Identifier::generate();
  103.                     $command->name 'Toast Lover';
  104.                     $command->surname '';
  105.                     $command->email $email;
  106.                     $command->username $email;
  107.                     $command->emailConfirmed true;
  108.                     $command->registrationType UserProfile::REGISTRATION_TYPE_APPLE;
  109.                     $command->appleSignInSub $payload->sub;
  110.                     $commandBus->execute($command);
  111.                     $user $userRepository->find($command->id);
  112.                     return $authManager->manualLogin($user'registration_finish_registration');
  113.                 }
  114.             } else {
  115.                 $user $userRepository->findByAppleSignInSub($payload->sub);
  116.                 if (null === $user) {
  117.                     throw new \RuntimeException('User was not found by apple sign in');
  118.                 }
  119.             }
  120.         } catch (\Exception $e) {
  121.             $logger->error($e->getMessage(), Log::SOURCE_APPLE, ['trace' => $e->getTraceAsString()]);
  122.             throw new \RuntimeException('Failed to auth with apple');
  123.         }
  124.         return $authManager->manualLogin($user'user_ios_login_index');
  125.     }
  126. }