<?php
namespace App\Controller;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use KnpU\OAuth2ClientBundle\Client\Provider\AzureClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use TheNetworg\OAuth2\Client\Provider\Azure;
class SecurityOauthAzureController extends AbstractController
{
/**
* Link to this controller to start the "connect" process
*
*/
#[Route(path: '/connect/azure', name:'connect_azure_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('azure') // key used in config/packages/knpu_oauth2_client.yaml
->redirect([
'public_profile', 'email' // the scopes you want to access
]);
}
/**
* 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(path: '/connect/azure/check', name:'connect_azure_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
// (read below)
/** @var AzureClient $client */
$client = $clientRegistry->getClient('azure');
try {
// the exact class depends on which provider you're using
/** @var Azure $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;
}
}
}