src/Controller/GoogleController.php line 46

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 League\OAuth2\Client\Provider\Google;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use App\Entity\Client;
  10. use App\Entity\Config;
  11. use App\Entity\UserContact;
  12. use App\Form\SocialRegistrationFormType;
  13. use App\Security\UserContactAuthenticator;
  14. use App\Services\CallApiServices;
  15. use App\Services\QuestionMailService;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  19. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  21. class GoogleController extends AbstractController
  22. {
  23.     /**
  24.      * Link to this controller to start the "connect" process
  25.      *
  26.      * @Route("/connect/google", name="connect_google_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('google'// 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/google", name="inscription_google_start")
  40.      */
  41.     public function googleInscriptionAction(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('googleRegister'// 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/google/check", name="connect_google_check")
  56.      */
  57.     public function connectCheckAction(Request $requestClientRegistry $clientRegistry,  AuthenticationUtils $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\Google $client */
  63.         $client $clientRegistry->getClient('google');
  64.     
  65.         try {
  66.             // the exact class depends on which provider you're using
  67.             /** @var \League\OAuth2\Client\Provider\Google $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('clientGoogle'$user);
  74.         // if ($this->getUser()) {
  75.         //     return $this->redirectToRoute('target_path');
  76.         // }
  77.         // get the login error if there is one
  78.         $error $authenticationUtils->getLastAuthenticationError();
  79.         // last username entered by the user
  80.         $lastUsername $user->getEmail();
  81.         $password $user->getId();
  82.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  83.         } catch (IdentityProviderException $e) {
  84.             // something went wrong!
  85.             // probably you should return the reason to the user
  86.             var_dump($e->getMessage());
  87.             die;
  88.         }
  89.     }
  90.      /**
  91.      * After going to Google, you're redirected back here
  92.      * because this is the "redirect_route" you configured
  93.      * in config/packages/knpu_oauth2_client.yaml
  94.      *
  95.      * @Route("/register/google/check", name="register_google_check")
  96.      */
  97.     public function registerCheckAction(Request $requestClientRegistry $clientRegistry)
  98.     {
  99.         $clientGoogle = new Client();
  100.         // ** if you want to *authenticate* the user, then
  101.         // leave this method blank and create a Guard authenticator
  102.         // (read below)
  103.         /** @var \KnpU\OAuth2ClientBundle\Client\Provider\Google $client */
  104.         $client $clientRegistry->getClient('googleRegister');
  105.     
  106.         try {
  107.             // the exact class depends on which provider you're using
  108.             /** @var \League\OAuth2\Client\Provider\Google $user */
  109.             $user $client->fetchUser();
  110.             // do something with all this new power!
  111.             $clientGoogle->setFirstName($user->getFirstName());
  112.             $clientGoogle->setLastName($user->getLastName()); 
  113.             $clientGoogle->setEmail($user->getEmail()); 
  114.             $clientGoogle->setSocial('google');
  115.             $clientGoogle->setSocialUid($user->getId());
  116.            
  117.             $session $request->getSession();
  118.             $session->set('clientGoogle'$clientGoogle);
  119.             
  120.         return $this->redirectToRoute("app_google_social_register");
  121.             // ...
  122.         } catch (IdentityProviderException $e) {
  123.             // something went wrong!
  124.             // probably you should return the reason to the user
  125.             var_dump($e->getMessage());
  126.             die;
  127.         }
  128.      
  129.     }
  130.     
  131.     /**
  132.      * @Route("/socialLog", name="app_social_login")
  133.      */
  134.     public function googleLogin(CallApiServices $callApiServicesQuestionMailService $questionMailServiceRequest $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorUserContactAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  135.     {
  136.         // if ($this->getUser()) {
  137.         //     return $this->redirectToRoute('target_path');
  138.         // }
  139.         // get the login error if there is one
  140.         $error $authenticationUtils->getLastAuthenticationError();
  141.         // last username entered by the user
  142.         $lastUsername $authenticationUtils->getLastUsername();
  143.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  144.     }
  145.         /**
  146.      * @Route("/googleSocial", name="app_google_social_register")
  147.      */
  148.     public function googleRegister(CallApiServices $callApiServicesQuestionMailService $questionMailServiceRequest $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorUserContactAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  149.     {
  150.         $serviceId $this->getParameter('app.serviceId');
  151.         $accountId $this->getParameter('app.accountId');
  152.         $user = new Client();
  153.         $session $request->getSession();
  154.         $clientGoogle $session->get('clientGoogle');
  155.         $config $callApiServices->configuratationPlateforme($accountId$serviceId);
  156.         $recaptchaKey $config['recaptchaTab']['sitekey'];
  157.         $form $this->createForm(SocialRegistrationFormType::class, $user);
  158.         $session $request->getSession();
  159.         $addressIp =$request->getClientIp();
  160.         if ($session->get('parrainId') == null){
  161.             $parrainId "0"
  162.         }else{
  163.             $parrainId $session->get('parrainId');
  164.         }
  165.        
  166.         $form->get('firstName')->setData($clientGoogle->getFirstName());
  167.         $form->get('lastName')->setData($clientGoogle->getLastName());
  168.         $form->get('email')->setData($clientGoogle->getEmail());
  169.     
  170.     
  171.         $form->handleRequest($request);
  172.         if ($form->isSubmitted() && $form->isValid()) {
  173.             $password $clientGoogle->getSocialUid();
  174.             $user->setSocial($clientGoogle->getSocial());
  175.             $user->setSocialUid($clientGoogle->getSocialUid());
  176.             $user->setPassword($password);
  177.             $clientInscription $callApiServices->socialInscription(
  178.                 $serviceId,
  179.                 $parrainId,
  180.                 $user->getGenre(),
  181.                 $user->getEmail(),
  182.                 $user->getPassword(),
  183.                 $user->getFirstName(),
  184.                 $user->getLastName(),
  185.                 $user->getCountry(),
  186.                 $user->getPhoneNumber(),
  187.                 $user->getSocial(),
  188.                 $user->getSocialUid(),
  189.                 $addressIp
  190.     
  191.             );
  192.             $user->setCode($clientInscription['code']);
  193.             $user->setServiceId($serviceId);
  194.            
  195.             $session->clear();
  196.             $this->addFlash("inscription""Vous avez bien été inscrit! Vous allez recevoir un mail afin d'activer votre compte d'un moment à l'autre.
  197.             Si vous ne recevez pas le mail, veuillez vérifier dans votre boîte SPAM ou courrier indésirable, s'il s'y trouve !! ");
  198.             // Envoie de Mail.
  199.             //$questionMailService->checkMail($user, $request);
  200.             return $this->redirectToRoute("app_login");
  201.          
  202.         }
  203.         return $this->render('registration/social_register.html.twig', [
  204.             'registrationForm' => $form->createView(),
  205.             "recaptchaKey" =>$recaptchaKey,
  206.         ]);
  207.     }
  208.     
  209. }