src/Controller/FacebookController.php line 18

Open in your IDE?
  1. <?php 
  2. namespace App\Controller;
  3. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  4. use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Repository\UserRepository;
  9. class FacebookController extends AbstractController
  10. {
  11.     /**
  12.      * Link to this controller to start the "connect" process
  13.      *
  14.      * @Route("/connect/facebook", name="connect_facebook_start")
  15.      */
  16.     public function connectAction(ClientRegistry $clientRegistry)
  17.     {
  18.         // on Symfony 3.3 or lower, $clientRegistry = $this->get('knpu.oauth2.registry');
  19.         // will redirect to Google!
  20.         return $clientRegistry
  21.             ->getClient('facebook') // key used in config/packages/knpu_oauth2_client.yaml
  22.             ->redirect([
  23.                 'public_profile', 'email' // the scopes you want to access
  24.             ]);
  25.     }
  26.     /**
  27.      * After going to Facebook, you're redirected back here
  28.      * because this is the "redirect_route" you configured
  29.      * in config/packages/knpu_oauth2_client.yaml
  30.      *
  31.      * @Route("/connect/facebook/check", name="connect_facebook_check")
  32.      */
  33.     public function connectCheckAction(Request $request, ClientRegistry $clientRegistry, UserRepository $userRepository )
  34.     {
  35.         // ** if you want to *authenticate* the user, then
  36.         // leave this method blank and create a Guard authenticator
  37.         // (read below)
  38.         /** @var \KnpU\OAuth2ClientBundle\Client\Provider\FacebookClient $client */
  39.         $client = $clientRegistry->getClient('facebook');
  40.         //dd($client->fetchUser());
  41.         try {
  42.             // the exact class depends on which provider you're using
  43.             /** @var \League\OAuth2\Client\Provider\FacebookUser $user */
  44.             $user = $client->fetchUser();
  45.             
  46.             $findUserByEmail =$userRepository->findOneBy(['email' => $user->getEmail()]);
  47.             //dd($findUserByEmail);
  48.             if($findUserByEmail == null) {
  49.                 $this->addFlash('app_event_login', 'User not found!');
  50.                 return $this->redirectToRoute('app_register');
  51.             }
  52.             if(empty($findUserByEmail->getProvider()) || $findUserByEmail->getProvider() != 'Facebook' ) {
  53.                 $findUserByEmail->setProviderId($user->getID());
  54.                 $findUserByEmail->setProvider('Facebook');
  55.                 $dateNow = new \DateTime();
  56.                 $findUserByEmail->setUpdatedAt($dateNow);
  57.             }
  58.             //dd($findUserByEmail);
  59.             // do something with all this new power!
  60.             // e.g. $name = $user->getFirstName();
  61.             
  62.             // ...
  63.             $userRepository->save($findUserByEmail, true);
  64.             return $user;
  65.         } catch (IdentityProviderException $e) {
  66.             // something went wrong!
  67.             // probably you should return the reason to the user
  68.             var_dump($e->getMessage()); die;
  69.         }
  70.     }
  71. }