<?php
namespace App\Controller\Public;
use App\Exceptions\GenericException;
use App\Interface\UseCase\MicrosoftLoginCheckInterface;
use App\Security\WindowsOAuthProvider;
use Google\Client as GoogleClient;
use League\OAuth2\Client\Provider\GenericProvider;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController
{
public function __construct(
private WindowsOAuthProvider $windowsOAuthProvider,
private TokenStorageInterface $tokenStorage,
private ParameterBagInterface $parameterBag
) {}
#[Route('/', name: 'root')]
#[Route('/login', name: 'login')]
public function index(AuthenticationUtils $authenticationUtils): Response
{
return $this->render('login/index.html.twig', [
'last_username' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError(),
]);
}
#[Route('/logout', name: 'logout')]
public function logout(): void
{
}
#[Route('/auth/google', name: 'google_auth')]
public function googleLogin(Request $request)
{
$googleClient = new GoogleClient();
$googleClient->setClientId(
$this->getParameter('google')['oauth2']['credentials']['client_id']
);
$googleClient->setClientSecret(
$this->getParameter('google')['oauth2']['credentials']['client_secret']
);
$googleClient->setRedirectUri(
$this->generateUrl('google_auth', [], UrlGeneratorInterface::ABSOLUTE_URL)
);
$googleClient->addScope(['email', 'profile']);
if ($request->query->get('code')) {
$googleClient->fetchAccessTokenWithAuthCode($request->query->get('code'));
$googleOauth = new \Google_Service_Oauth2($googleClient);
$userInfo = $googleOauth->userinfo->get();
dump($userInfo);
exit;
}
return $this->redirect($googleClient->createAuthUrl());
}
#[Route('/auth/microsoft', name: 'auth_microsoft')]
public function microsoftLogin(Request $request, MicrosoftLoginCheckInterface $microsoftLoginCheckUseCase): Response
{
$provider = $microsoftLoginCheckUseCase->getMicrosoftProvider();
$authUrl = $provider->getAuthorizationUrl();
$request->getSession()->set('oauth2state', $provider->getState());
return $this->redirect($authUrl);
}
#[Route('/auth/microsoft/check', name: 'auth_microsoft_check')]
public function microsoftCheck(MicrosoftLoginCheckInterface $microsoftLoginCheckUseCase): Response
{
$redirect = 'root';
try {
$microsoftLoginCheckUseCase->check();
$redirect = "app_home";
} catch (GenericException $e) {
$this->addFlash('warning', $e->getMessage());
} catch (\Throwable $th) {
$this->addFlash('error', $th->getMessage());
}
return $this->redirectToRoute($redirect);
}
}