<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
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 FacebookController extends AbstractController
{
private $facebookInscription = [];
/**
* Link to this controller to start the "connect" process
*
* @Route("/connect/facebook", name="connect_facebook_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('facebook') // key used in config/packages/knpu_oauth2_client.yaml
->redirect();
}
/**
* Link to this controller to start the "connect" process
*
* @Route("/register/facebook", name="inscription_facebook_start")
*/
public function inscriptionAction(ClientRegistry $clientRegistry)
{
// on Symfony 3.3 or lower, $clientRegistry = $this->get('knpu.oauth2.registry');
// will redirect to Facebook!
return $clientRegistry
->getClient('facebookRegister') // 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/facebook/check", name="connect_facebook_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\Facebook $client */
$client = $clientRegistry->getClient('facebook');
try {
// the exact class depends on which provider you're using
/** @var \League\OAuth2\Client\Provider\Facebook $user */
$user = $client->fetchUser();
// do something with all this new power!
// e.g. $name = $user->getFirstName();
$session = $request->getSession();
$session->set('clientFacebook', $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;
}
}
/**
* @Route("/facebookLog", name="app_facebook_login")
*/
public function facebookLogin(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]);
}
/**
* 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("/register/facebook/check", name="register_facebook_check")
*/
public function registerCheckAction(Request $request, ClientRegistry $clientRegistry)
{
$clientFacebook = new Client();
// ** if you want to *authenticate* the user, then
// leave this method blank and create a Guard authenticator
// (read below)
$clientRegistry = $clientRegistry->getClient('facebookRegister');
/** @var \KnpU\OAuth2ClientBundle\Client\Provider\Facebook $client */
try {
// the exact class depends on which provider you're using
/** @var \League\OAuth2\Client\Provider\Facebook $user */
$user = $clientRegistry->fetchUser();
// do something with all this new power!
$clientFacebook->setFirstName($user->getFirstName());
$clientFacebook->setLastName($user->getLastName());
$clientFacebook->setEmail($user->getEmail());
$clientFacebook->setSocial('facebook');
$clientFacebook->setSocialUid($user->getId());
$session = $request->getSession();
$session->set('clientFacebook', $clientFacebook);
return $this->redirectToRoute("app_facebook_register");
// return $this->redirectToRoute('app_facebook_register');
} catch (IdentityProviderException $e) {
// something went wrong!
// probably you should return the reason to the user
var_dump($e->getMessage());
die;
}
// return $userAuthenticator->authenticateUser(
// $user,
// $authenticator,
// $request
// );
}
/**
* @Route("/facebook", name="app_facebook_register")
*/
public function facebookRegister(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();
$clientFacebook = $session->get('clientFacebook');
$config = $callApiServices->configuratationPlateforme($accountId, $serviceId);
$recaptchaKey = $config['recaptchaTab']['sitekey'];
$form = $this->createForm(SocialRegistrationFormType::class, $user);
$form->get('firstName')->setData($clientFacebook->getFirstName());
$form->get('lastName')->setData($clientFacebook->getLastName());
$form->get('email')->setData($clientFacebook->getEmail());
$session = $request->getSession();
$addressIp =$request->getClientIp();
if ($session->get('parrainId') == null){
$parrainId = "0";
}else{
$parrainId = $session->get('parrainId');
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$password = $clientFacebook->getSocialUid();
$user->setSocial($clientFacebook->getSocial());
$user->setSocialUid($clientFacebook->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($clientInscription['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);
return $this->redirectToRoute("app_login");
}
return $this->render('registration/social_register.html.twig', [
'registrationForm' => $form->createView(),
"recaptchaKey" =>$recaptchaKey,
]);
}
}