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