src/Http/Controller/Auth/GoogleController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Http\Controller\Auth;
  3. use App\Application\CommandBus\Command\User\RegistrationCommand;
  4. use App\Database\Domain\Entity\User\UserProfile;
  5. use App\Database\Domain\Repository\UserRepository;
  6. use App\Infrastructure\Messenger\CommandBus\CommandBusInterface;
  7. use App\Infrastructure\Security\AuthManager;
  8. use App\Infrastructure\Service\GoogleHelper;
  9. use App\Infrastructure\Util\Identifier;
  10. use Google\Service\Oauth2;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @Route("", name="google_")
  17.  */
  18. class GoogleController extends AbstractController
  19. {
  20.     /**
  21.      * @Route("/google/login", name="login")
  22.      */
  23.     public function __invoke(GoogleHelper $googleHelperRequest $request): \Symfony\Component\HttpFoundation\RedirectResponse
  24.     {
  25.         $request->getSession()->set('redirect-uri'$request->query->get('redirect-uri'));
  26.         return $this->redirect($googleHelper->login());
  27.     }
  28.     /**
  29.      * @Route("/user-google/login-webhook", name="login_handle")
  30.      */
  31.     public function handleLogin(
  32.         GoogleHelper $googleHelper,
  33.         CommandBusInterface $commandBus,
  34.         Request $request,
  35.         UserRepository $userRepository,
  36.         AuthManager $authManager
  37.     ): Response {
  38.         $code $request->query->get('code');
  39.         $googleInstance $googleHelper->getInstance();
  40.         // Fetch the access token using the authorization code
  41.         $token $googleInstance->fetchAccessTokenWithAuthCode($code);
  42.         // Check for errors
  43.         if (array_key_exists('error'$token)) {
  44.             throw new \Exception(sprintf('Failed to authenticate with Google: %s'$token['error_description'] ?? $token['error']));
  45.         }
  46.         $googleInstance->setAccessToken($token);
  47.         // Use OAuth2 service instead of Google Plus
  48.         $oauth2Service = new Oauth2($googleInstance);
  49.         $googleProfile $oauth2Service->userinfo->get();
  50.         // Get user information from the new API
  51.         $email $googleProfile->getEmail();
  52.         $user $userRepository->findByEmail($email);
  53.         $redirectParams = [];
  54.         if (!$user) {
  55.             // Extract user information from OAuth2 response
  56.             $name $googleProfile->getGivenName() ?: '';
  57.             $surname $googleProfile->getFamilyName() ?: '';
  58.             $fullName $googleProfile->getName() ?: $email;
  59.             // If name parts are empty, try to split the full name
  60.             if (empty($name) && empty($surname) && !empty($fullName)) {
  61.                 $nameParts explode(' '$fullName);
  62.                 $name $nameParts[0] ?? $email;
  63.                 $surname $nameParts[1] ?? null;
  64.             }
  65.             $command = new RegistrationCommand();
  66.             $command->id Identifier::generate();
  67.             $command->name $name ?: $email;
  68.             $command->surname $surname;
  69.             $command->avatarUrl $googleProfile->getPicture();
  70.             $command->email $email;
  71.             $command->username $email;
  72.             $command->emailConfirmed true;
  73.             $command->registrationType UserProfile::REGISTRATION_TYPE_GOOGLE;
  74.             $commandBus->execute($command);
  75.             $user $userRepository->find($command->id);
  76.             $redirectParams = ["source" => "registered"];
  77.         }
  78.         if ($request->getSession()->get('redirect-uri') == 'ios-login') {
  79.             return $authManager->manualLogin($user'user_ios_login_index');
  80.         }
  81.         return $authManager->manualLogin($user"user_sessions_index"$redirectParams);
  82.     }
  83. }