src/Http/Controller/Auth/FacebookController.php line 21

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\FacebookHelper;
  9. use App\Infrastructure\Util\Identifier;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route("", name="facebook_")
  16.  */
  17. class FacebookController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/facebook/login", name="login")
  21.      */
  22.     public function __invoke(FacebookHelper $facebookHelper): \Symfony\Component\HttpFoundation\RedirectResponse
  23.     {
  24.         return $this->redirect($facebookHelper->login());
  25.     }
  26.     /**
  27.      * @Route("/user-facebook/login-webhook", name="login_handle")
  28.      */
  29.     public function handleLogin(
  30.         Request $request,
  31.         FacebookHelper $facebookHelper,
  32.         CommandBusInterface $commandBus,
  33.         UserRepository $userRepository,
  34.         AuthManager $authManager
  35.     ): Response {
  36.         $fbInstance $facebookHelper->makeInstance();
  37.         $loginHelper $fbInstance->getRedirectLoginHelper();
  38.         $accessToken $loginHelper->getAccessToken();
  39.         if (null === $accessToken) {
  40.             throw new \RuntimeException('Access token is missing');
  41.         }
  42.         $fbProfile $fbInstance->get('/me?fields=id,first_name,email,last_name'$accessToken)->getGraphUser();
  43.         if (!$fbProfile->getEmail()) {
  44.             $request->getSession()->set('facebook_error''You must specify email address on your facebook account to continue. https://www.facebook.com/settings');
  45.             return $this->redirectToRoute('user_login');
  46.         }
  47.         $user $userRepository->findByEmail($fbProfile->getEmail());
  48.         if (!$user) {
  49.             $command = new RegistrationCommand();
  50.             $command->id Identifier::generate();
  51.             $command->name $fbProfile->getName() ?: $fbProfile->getEmail();
  52.             $command->surname $fbProfile->getLastName();
  53.             $command->avatarUrl 'https://graph.facebook.com/'.$fbProfile->getId().'/picture?type=large';
  54.             $command->email $fbProfile->getEmail();
  55.             $command->username $fbProfile->getEmail();
  56.             $command->emailConfirmed true;
  57.             $command->registrationType UserProfile::REGISTRATION_TYPE_FACEBOOK;
  58.             $commandBus->execute($command);
  59.             $user $userRepository->find($command->id);
  60.             return $authManager->manualLogin($user'registration_finish_registration');
  61.         }
  62.         return $authManager->manualLogin($user);
  63.     }
  64. }