src/Controller/TinyKnightGames/Authentication/AuthenticationController.php line 14

  1. <?php
  2. namespace App\Controller\TinyKnightGames\Authentication;
  3. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  4. use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. class AuthenticationController extends AbstractController
  8. {
  9.     #[Route('/connect/google'name'connect_google_start')]
  10.     public function connectGoogleStart(ClientRegistry $clientRegistry)
  11.     {
  12.         return $clientRegistry
  13.             ->getClient('google_main'// key used in config/packages/knpu_oauth2_client.yaml
  14.             ->redirect([
  15.                 'profile''email' // the scopes you want to access
  16.             ]);
  17.     }
  18.     #[Route('/connect/google/check'name'connect_google_check')]
  19.     public function connectGoogleCheck(ClientRegistry $clientRegistry)
  20.     {
  21.         $client $clientRegistry->getClient('google_main');
  22.         try {
  23.             /** @var \League\OAuth2\Client\Provider\GoogleUser */
  24.             $user $client->fetchUser();
  25.         } catch (IdentityProviderException $e) {
  26.             var_dump($e->getMessage());
  27.             die();
  28.         }
  29.         return $clientRegistry
  30.             ->getClient('google_main'// key used in config/packages/knpu_oauth2_client.yaml
  31.             ->redirect([
  32.                 'profile''email' // the scopes you want to access
  33.             ]);
  34.     }
  35. }