<?php
namespace App\Http\Controller\Auth;
use App\Application\CommandBus\Command\User\RegistrationCommand;
use App\Database\Domain\Entity\User\UserProfile;
use App\Database\Domain\Repository\UserRepository;
use App\Infrastructure\Messenger\CommandBus\CommandBusInterface;
use App\Infrastructure\Security\AuthManager;
use App\Infrastructure\Service\GoogleHelper;
use App\Infrastructure\Util\Identifier;
use Google\Service\Oauth2;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("", name="google_")
*/
class GoogleController extends AbstractController
{
/**
* @Route("/google/login", name="login")
*/
public function __invoke(GoogleHelper $googleHelper, Request $request): \Symfony\Component\HttpFoundation\RedirectResponse
{
$request->getSession()->set('redirect-uri', $request->query->get('redirect-uri'));
return $this->redirect($googleHelper->login());
}
/**
* @Route("/user-google/login-webhook", name="login_handle")
*/
public function handleLogin(
GoogleHelper $googleHelper,
CommandBusInterface $commandBus,
Request $request,
UserRepository $userRepository,
AuthManager $authManager
): Response {
$code = $request->query->get('code');
$googleInstance = $googleHelper->getInstance();
// Fetch the access token using the authorization code
$token = $googleInstance->fetchAccessTokenWithAuthCode($code);
// Check for errors
if (array_key_exists('error', $token)) {
throw new \Exception(sprintf('Failed to authenticate with Google: %s', $token['error_description'] ?? $token['error']));
}
$googleInstance->setAccessToken($token);
// Use OAuth2 service instead of Google Plus
$oauth2Service = new Oauth2($googleInstance);
$googleProfile = $oauth2Service->userinfo->get();
// Get user information from the new API
$email = $googleProfile->getEmail();
$user = $userRepository->findByEmail($email);
$redirectParams = [];
if (!$user) {
// Extract user information from OAuth2 response
$name = $googleProfile->getGivenName() ?: '';
$surname = $googleProfile->getFamilyName() ?: '';
$fullName = $googleProfile->getName() ?: $email;
// If name parts are empty, try to split the full name
if (empty($name) && empty($surname) && !empty($fullName)) {
$nameParts = explode(' ', $fullName);
$name = $nameParts[0] ?? $email;
$surname = $nameParts[1] ?? null;
}
$command = new RegistrationCommand();
$command->id = Identifier::generate();
$command->name = $name ?: $email;
$command->surname = $surname;
$command->avatarUrl = $googleProfile->getPicture();
$command->email = $email;
$command->username = $email;
$command->emailConfirmed = true;
$command->registrationType = UserProfile::REGISTRATION_TYPE_GOOGLE;
$commandBus->execute($command);
$user = $userRepository->find($command->id);
$redirectParams = ["source" => "registered"];
}
if ($request->getSession()->get('redirect-uri') == 'ios-login') {
return $authManager->manualLogin($user, 'user_ios_login_index');
}
return $authManager->manualLogin($user, "user_sessions_index", $redirectParams);
}
}