src/Controller/FacebookController.php line 28
<?php
namespace App\Controller;
use App\Entity\GamePlay;
use App\Entity\Popin;
use App\Entity\UserChallengeReward;
use Doctrine\Persistence\ManagerRegistry;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class FacebookController extends AbstractController
{
private $doctrine;
private $entityManager;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
$this->entityManager = $this->doctrine->getManager();
}
#[Route('/', name: 'homepage_base')]
public function index(): Response
{
return $this->redirectToRoute('homepage');
}
#[Route('/connect/fb', name: 'connect_facebook')]
public function connectAction(ClientRegistry $clientRegistry)
{
//Redirect to google
return $clientRegistry->getClient('facebook')->redirect(['public_profile', 'email'], []);
}
/**
* 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('/connect/fb/check', name: 'connect_facebook_check')]
public function connectCheckAction(Request $request, ClientRegistry $clientRegistry)
{
// ** if you want to *authenticate* the user, then
// leave this method blank and create a Guard authenticator
$client = $clientRegistry->getClient('facebook');
try {
// the exact class depends on which provider you're using
/** @var \League\OAuth2\Client\Provider\FacebookUser $user */
$user = $client->fetchUser();
// do something with all this new power!
// e.g. $name = $user->getFirstName();
var_dump($user); die;
// ...
} catch (IdentityProviderException $e) {
// something went wrong!
// probably you should return the reason to the user
var_dump($e->getMessage()); die;
}
}
}