<?php
namespace App\Controller\Bo;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use KnpU\OAuth2ClientBundle\Client\Provider\GoogleClient;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Provider\GoogleUser;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
class GoogleController extends AbstractController
{
#[Route('/connect/google', name: 'connect_google')]
public function connectAction(ClientRegistry $clientRegistry)
{
return $clientRegistry
->getClient('google')
->redirect();
}
#[Route('/connect/google/check', name: 'connect_google_check')]
public function connectCheckAction(ClientRegistry $clientRegistry): RedirectResponse
{
/** @var GoogleClient $client */
$client = $clientRegistry->getClient('google');
try {
/** @var GoogleUser $user */
$user = $client->fetchUser();
var_dump($user); die;
} catch (IdentityProviderException $e) {
var_dump($e->getMessage()); die;
}
}
}