<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Provider\Google;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Client;
use App\Entity\Config;
use App\Entity\UserContact;
use App\Form\SocialRegistrationFormType;
use App\Security\UserContactAuthenticator;
use App\Services\CallApiServices;
use App\Services\QuestionMailService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class GoogleController extends AbstractController
{
/**
* Link to this controller to start the "connect" process
*
* @Route("/connect/google", name="connect_google_start")
*/
public function connectAction(ClientRegistry $clientRegistry)
{
// on Symfony 3.3 or lower, $clientRegistry = $this->get('knpu.oauth2.registry');
// will redirect to Facebook!
return $clientRegistry
->getClient('google') // key used in config/packages/knpu_oauth2_client.yaml
->redirect();
}
/**
* Link to this controller to start the "connect" process
*
* @Route("/register/google", name="inscription_google_start")
*/
public function googleInscriptionAction(ClientRegistry $clientRegistry)
{
// on Symfony 3.3 or lower, $clientRegistry = $this->get('knpu.oauth2.registry');
// will redirect to Facebook!
return $clientRegistry
->getClient('googleRegister') // key used in config/packages/knpu_oauth2_client.yaml
->redirect();
}
/**
* After going to Facebook, you're redirected back here
* because this is the "redirect_route" you configured
* in config/packages/knpu_oauth2_client.yaml
*
* @Route("/connect/google/check", name="connect_google_check")
*/
public function connectCheckAction(Request $request, ClientRegistry $clientRegistry, AuthenticationUtils $authenticationUtils)
{
// ** if you want to *authenticate* the user, then
// leave this method blank and create a Guard authenticator
// (read below)
/** @var \KnpU\OAuth2ClientBundle\Client\Provider\Google $client */
$client = $clientRegistry->getClient('google');
try {
// the exact class depends on which provider you're using
/** @var \League\OAuth2\Client\Provider\Google $user */
$user = $client->fetchUser();
// do something with all this new power!
// e.g. $name = $user->getFirstName();
$session = $request->getSession();
$session->set('clientGoogle', $user);
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $user->getEmail();
$password = $user->getId();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
} catch (IdentityProviderException $e) {
// something went wrong!
// probably you should return the reason to the user
var_dump($e->getMessage());
die;
}
}
/**
* After going to Google, you're redirected back here
* because this is the "redirect_route" you configured
* in config/packages/knpu_oauth2_client.yaml
*
* @Route("/register/google/check", name="register_google_check")
*/
public function registerCheckAction(Request $request, ClientRegistry $clientRegistry)
{
$clientGoogle = new Client();
// ** if you want to *authenticate* the user, then
// leave this method blank and create a Guard authenticator
// (read below)
/** @var \KnpU\OAuth2ClientBundle\Client\Provider\Google $client */
$client = $clientRegistry->getClient('googleRegister');
try {
// the exact class depends on which provider you're using
/** @var \League\OAuth2\Client\Provider\Google $user */
$user = $client->fetchUser();
// do something with all this new power!
$clientGoogle->setFirstName($user->getFirstName());
$clientGoogle->setLastName($user->getLastName());
$clientGoogle->setEmail($user->getEmail());
$clientGoogle->setSocial('google');
$clientGoogle->setSocialUid($user->getId());
$session = $request->getSession();
$session->set('clientGoogle', $clientGoogle);
return $this->redirectToRoute("app_google_social_register");
// ...
} catch (IdentityProviderException $e) {
// something went wrong!
// probably you should return the reason to the user
var_dump($e->getMessage());
die;
}
}
/**
* @Route("/socialLog", name="app_social_login")
*/
public function googleLogin(CallApiServices $callApiServices, QuestionMailService $questionMailService, Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, UserContactAuthenticator $authenticator, EntityManagerInterface $entityManager): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/googleSocial", name="app_google_social_register")
*/
public function googleRegister(CallApiServices $callApiServices, QuestionMailService $questionMailService, Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, UserContactAuthenticator $authenticator, EntityManagerInterface $entityManager): Response
{
$serviceId = $this->getParameter('app.serviceId');
$accountId = $this->getParameter('app.accountId');
$user = new Client();
$session = $request->getSession();
$clientGoogle = $session->get('clientGoogle');
$config = $callApiServices->configuratationPlateforme($accountId, $serviceId);
$recaptchaKey = $config['recaptchaTab']['sitekey'];
$form = $this->createForm(SocialRegistrationFormType::class, $user);
$session = $request->getSession();
$addressIp =$request->getClientIp();
if ($session->get('parrainId') == null){
$parrainId = "0";
}else{
$parrainId = $session->get('parrainId');
}
$form->get('firstName')->setData($clientGoogle->getFirstName());
$form->get('lastName')->setData($clientGoogle->getLastName());
$form->get('email')->setData($clientGoogle->getEmail());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$password = $clientGoogle->getSocialUid();
$user->setSocial($clientGoogle->getSocial());
$user->setSocialUid($clientGoogle->getSocialUid());
$user->setPassword($password);
$clientInscription = $callApiServices->socialInscription(
$serviceId,
$parrainId,
$user->getGenre(),
$user->getEmail(),
$user->getPassword(),
$user->getFirstName(),
$user->getLastName(),
$user->getCountry(),
$user->getPhoneNumber(),
$user->getSocial(),
$user->getSocialUid(),
$addressIp
);
$user->setCode($clientInscription['code']);
$user->setServiceId($serviceId);
$session->clear();
$this->addFlash("inscription", "Vous avez bien été inscrit! Vous allez recevoir un mail afin d'activer votre compte d'un moment à l'autre.
Si vous ne recevez pas le mail, veuillez vérifier dans votre boîte SPAM ou courrier indésirable, s'il s'y trouve !! ");
// Envoie de Mail.
//$questionMailService->checkMail($user, $request);
return $this->redirectToRoute("app_login");
}
return $this->render('registration/social_register.html.twig', [
'registrationForm' => $form->createView(),
"recaptchaKey" =>$recaptchaKey,
]);
}
}