src/Controller/FacebookController.php line 28

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\GamePlay;
  4. use App\Entity\Popin;
  5. use App\Entity\UserChallengeReward;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  8. use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class FacebookController extends AbstractController
  14. {
  15.     private $doctrine;
  16.     private $entityManager;
  17.     
  18.     public function __construct(ManagerRegistry $doctrine)
  19.     {
  20.         $this->doctrine $doctrine;
  21.         $this->entityManager $this->doctrine->getManager();
  22.     }
  23.     #[Route('/'name'homepage_base')]
  24.     public function index(): Response
  25.     {
  26.         return $this->redirectToRoute('homepage');
  27.     }
  28.     #[Route('/connect/fb'name'connect_facebook')]
  29.     public function connectAction(ClientRegistry $clientRegistry)
  30.     {
  31.         //Redirect to google
  32.         return $clientRegistry->getClient('facebook')->redirect(['public_profile''email'], []);
  33.     }
  34.     /**
  35.      * After going to Google, you're redirected back here
  36.      * because this is the "redirect_route" you configured
  37.      * in config/packages/knpu_oauth2_client.yaml
  38.      */
  39.     #[Route('/connect/fb/check'name'connect_facebook_check')]
  40.     public function connectCheckAction(Request $requestClientRegistry $clientRegistry)
  41.     {
  42.         // ** if you want to *authenticate* the user, then
  43.         // leave this method blank and create a Guard authenticator
  44.         $client $clientRegistry->getClient('facebook');
  45.         try {
  46.             // the exact class depends on which provider you're using
  47.             /** @var \League\OAuth2\Client\Provider\FacebookUser $user */
  48.             $user $client->fetchUser();
  49.             // do something with all this new power!
  50.             // e.g. $name = $user->getFirstName();
  51.             var_dump($user); die;
  52.             // ...
  53.         } catch (IdentityProviderException $e) {
  54.             // something went wrong!
  55.             // probably you should return the reason to the user
  56.             var_dump($e->getMessage()); die;
  57.         }
  58.     }
  59. }