src/Controller/SecurityOauthAzureController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  4. use KnpU\OAuth2ClientBundle\Client\Provider\AzureClient;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use TheNetworg\OAuth2\Client\Provider\Azure;
  9. class SecurityOauthAzureController extends AbstractController
  10. {
  11.     /**
  12.      * Link to this controller to start the "connect" process
  13.      *
  14.      */
  15.     #[Route(path'/connect/azure'name:'connect_azure_start')]
  16.     public function connectAction(ClientRegistry $clientRegistry)
  17.     {
  18.         // on Symfony 3.3 or lower, $clientRegistry = $this->get('knpu.oauth2.registry');
  19.         // will redirect to Facebook!
  20.         return $clientRegistry
  21.             ->getClient('azure'// key used in config/packages/knpu_oauth2_client.yaml
  22.             ->redirect([
  23.                 'public_profile''email' // the scopes you want to access
  24.             ]);
  25.     }
  26.     /**
  27.      * After going to Facebook, you're redirected back here
  28.      * because this is the "redirect_route" you configured
  29.      * in config/packages/knpu_oauth2_client.yaml
  30.      **/
  31.     #[Route(path'/connect/azure/check'name:'connect_azure_check')]
  32.     public function connectCheckAction(Request $requestClientRegistry $clientRegistry)
  33.     {
  34.         // ** if you want to *authenticate* the user, then
  35.         // leave this method blank and create a Guard authenticator
  36.         // (read below)
  37.         /** @var AzureClient $client */
  38.         $client $clientRegistry->getClient('azure');
  39.         try {
  40.             // the exact class depends on which provider you're using
  41.             /** @var Azure $user */
  42.             $user $client->fetchUser();
  43.             // do something with all this new power!
  44.             // e.g. $name = $user->getFirstName();
  45.             var_dump($user); die;
  46.             // ...
  47.         } catch (IdentityProviderException $e) {
  48.             // something went wrong!
  49.             // probably you should return the reason to the user
  50.             var_dump($e->getMessage()); die;
  51.         }
  52.     }
  53. }