src/Controller/FacebookController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  5. use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Entity\Client;
  9. use App\Entity\Config;
  10. use App\Entity\UserContact;
  11. use App\Form\SocialRegistrationFormType;
  12. use App\Security\UserContactAuthenticator;
  13. use App\Services\CallApiServices;
  14. use App\Services\QuestionMailService;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  19. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  20. class FacebookController extends AbstractController
  21. {
  22.     private $facebookInscription = [];
  23.     /**
  24.      * Link to this controller to start the "connect" process
  25.      *
  26.      * @Route("/connect/facebook", name="connect_facebook_start")
  27.      */
  28.     public function connectAction(ClientRegistry $clientRegistry)
  29.     {
  30.         // on Symfony 3.3 or lower, $clientRegistry = $this->get('knpu.oauth2.registry');
  31.         // will redirect to Facebook!
  32.         return $clientRegistry
  33.             ->getClient('facebook'// key used in config/packages/knpu_oauth2_client.yaml
  34.             ->redirect();
  35.     }
  36.       /**
  37.      * Link to this controller to start the "connect" process
  38.      *
  39.      * @Route("/register/facebook", name="inscription_facebook_start")
  40.      */
  41.     public function inscriptionAction(ClientRegistry $clientRegistry)
  42.     {
  43.         // on Symfony 3.3 or lower, $clientRegistry = $this->get('knpu.oauth2.registry');
  44.         // will redirect to Facebook!
  45.         return $clientRegistry
  46.             ->getClient('facebookRegister'// key used in config/packages/knpu_oauth2_client.yaml
  47.             ->redirect();
  48.     }
  49.     
  50.     /**
  51.      * After going to Facebook, you're redirected back here
  52.      * because this is the "redirect_route" you configured
  53.      * in config/packages/knpu_oauth2_client.yaml
  54.      *
  55.      * @Route("/connect/facebook/check", name="connect_facebook_check")
  56.      */
  57.     public function connectCheckAction(Request $requestClientRegistry $clientRegistryAuthenticationUtils $authenticationUtils)
  58.     { 
  59.         // ** if you want to *authenticate* the user, then
  60.         // leave this method blank and create a Guard authenticator
  61.         // (read below)
  62.         /** @var \KnpU\OAuth2ClientBundle\Client\Provider\Facebook $client */
  63.         $client $clientRegistry->getClient('facebook');
  64.     
  65.         try {
  66.             // the exact class depends on which provider you're using
  67.             /** @var \League\OAuth2\Client\Provider\Facebook $user */
  68.             $user $client->fetchUser();
  69.           
  70.             // do something with all this new power!
  71.             // e.g. $name = $user->getFirstName();
  72.             $session $request->getSession();
  73.             $session->set('clientFacebook'$user);
  74.             
  75.         // if ($this->getUser()) {
  76.         //     return $this->redirectToRoute('target_path');
  77.         // }
  78.         // get the login error if there is one
  79.         $error $authenticationUtils->getLastAuthenticationError();
  80.         // last username entered by the user
  81.         $lastUsername $user->getEmail();
  82.         $password $user->getId();
  83.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  84.     
  85.         } catch (IdentityProviderException $e) {
  86.             // something went wrong!
  87.             // probably you should return the reason to the user
  88.             var_dump($e->getMessage());
  89.             die;
  90.         }
  91.         
  92.     }
  93.       /**
  94.      * @Route("/facebookLog", name="app_facebook_login")
  95.      */
  96.     public function facebookLogin(CallApiServices $callApiServicesQuestionMailService $questionMailServiceRequest $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorUserContactAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  97.     {
  98.         // if ($this->getUser()) {
  99.         //     return $this->redirectToRoute('target_path');
  100.         // }
  101.         // get the login error if there is one
  102.         $error $authenticationUtils->getLastAuthenticationError();
  103.         // last username entered by the user
  104.         $lastUsername $authenticationUtils->getLastUsername();
  105.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  106.     }
  107.      /**
  108.      * After going to Facebook, you're redirected back here
  109.      * because this is the "redirect_route" you configured
  110.      * in config/packages/knpu_oauth2_client.yaml
  111.      *
  112.      * @Route("/register/facebook/check", name="register_facebook_check")
  113.      */
  114.     public function registerCheckAction(Request $requestClientRegistry $clientRegistry)
  115.     {
  116.         $clientFacebook = new Client();
  117.         // ** if you want to *authenticate* the user, then
  118.         // leave this method blank and create a Guard authenticator
  119.         // (read below)
  120.         $clientRegistry $clientRegistry->getClient('facebookRegister');
  121.     
  122.         /** @var \KnpU\OAuth2ClientBundle\Client\Provider\Facebook $client */
  123.         try {
  124.             // the exact class depends on which provider you're using
  125.             /** @var \League\OAuth2\Client\Provider\Facebook $user */
  126.             $user $clientRegistry->fetchUser();
  127.             
  128.             // do something with all this new power!
  129.             $clientFacebook->setFirstName($user->getFirstName());
  130.             $clientFacebook->setLastName($user->getLastName()); 
  131.             $clientFacebook->setEmail($user->getEmail()); 
  132.             $clientFacebook->setSocial('facebook');
  133.             $clientFacebook->setSocialUid($user->getId());
  134.            
  135.             $session $request->getSession();
  136.             $session->set('clientFacebook'$clientFacebook);
  137.             
  138.         return $this->redirectToRoute("app_facebook_register");
  139.             // return $this->redirectToRoute('app_facebook_register');
  140.         } catch (IdentityProviderException $e) {
  141.             // something went wrong!
  142.             // probably you should return the reason to the user
  143.             var_dump($e->getMessage());
  144.             die;
  145.         }
  146.             // return $userAuthenticator->authenticateUser(
  147.             //     $user,
  148.             //     $authenticator,
  149.             //     $request
  150.             // );
  151.     }
  152.        /**
  153.      * @Route("/facebook", name="app_facebook_register")
  154.      */
  155.     public function facebookRegister(CallApiServices $callApiServicesQuestionMailService $questionMailServiceRequest $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorUserContactAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  156.     {
  157.         $serviceId $this->getParameter('app.serviceId');
  158.         $accountId $this->getParameter('app.accountId');
  159.         $user = new Client();
  160.         $session $request->getSession();
  161.         $clientFacebook $session->get('clientFacebook');
  162.         $config $callApiServices->configuratationPlateforme($accountId$serviceId);
  163.         $recaptchaKey $config['recaptchaTab']['sitekey'];
  164.         $form $this->createForm(SocialRegistrationFormType::class, $user);
  165.         
  166.         $form->get('firstName')->setData($clientFacebook->getFirstName());
  167.         $form->get('lastName')->setData($clientFacebook->getLastName());
  168.         $form->get('email')->setData($clientFacebook->getEmail());
  169.         $session $request->getSession();
  170.         $addressIp =$request->getClientIp();
  171.         if ($session->get('parrainId') == null){
  172.             $parrainId "0"
  173.         }else{
  174.             $parrainId $session->get('parrainId');
  175.         }
  176.        
  177.         $form->handleRequest($request);
  178.         if ($form->isSubmitted() && $form->isValid()) {
  179.            $password $clientFacebook->getSocialUid();
  180.             $user->setSocial($clientFacebook->getSocial());
  181.             $user->setSocialUid($clientFacebook->getSocialUid());
  182.             $user->setPassword($password);
  183.             $clientInscription $callApiServices->socialInscription(
  184.                 $serviceId,
  185.                 $parrainId,
  186.                 $user->getGenre(),
  187.                 $user->getEmail(),
  188.                 $user->getPassword(),
  189.                 $user->getFirstName(),
  190.                 $user->getLastName(),
  191.                 $user->getCountry(),
  192.                 $user->getPhoneNumber(),
  193.                 $user->getSocial(),
  194.                 $user->getSocialUid(),
  195.                 $addressIp
  196.     
  197.             );
  198.             $user->setCode($clientInscription['code']);
  199.             $user->setServiceId($clientInscription['serviceId']);
  200.            
  201.             $session->clear();
  202.             $this->addFlash("inscription""Vous avez bien été inscrit! Vous allez recevoir un mail afin d'activer votre compte d'un moment à l'autre.
  203.             Si vous ne recevez pas le mail, veuillez vérifier dans votre boîte SPAM ou courrier indésirable, s'il s'y trouve !! ");
  204.             // Envoie de Mail.
  205.             //$questionMailService->checkMail($user);
  206.             return $this->redirectToRoute("app_login");
  207.          
  208.         }
  209.         return $this->render('registration/social_register.html.twig', [
  210.             'registrationForm' => $form->createView(),
  211.             "recaptchaKey" =>$recaptchaKey,
  212.         ]);
  213.     }
  214. }