src/Controller/Public/LoginController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Public;
  3. use App\Exceptions\GenericException;
  4. use App\Interface\UseCase\MicrosoftLoginCheckInterface;
  5. use App\Security\WindowsOAuthProvider;
  6. use Google\Client as GoogleClient;
  7. use League\OAuth2\Client\Provider\GenericProvider;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. class LoginController extends AbstractController
  18. {
  19.     public function __construct(
  20.         private WindowsOAuthProvider $windowsOAuthProvider,
  21.         private TokenStorageInterface $tokenStorage,
  22.         private ParameterBagInterface $parameterBag
  23.     ) {}
  24.     #[Route('/'name'root')]
  25.     #[Route('/login'name'login')]
  26.     public function index(AuthenticationUtils $authenticationUtils): Response
  27.     {
  28.         return $this->render('login/index.html.twig', [
  29.             'last_username' => $authenticationUtils->getLastUsername(),
  30.             'error'         => $authenticationUtils->getLastAuthenticationError(),
  31.         ]);
  32.     }
  33.     #[Route('/logout'name'logout')]
  34.     public function logout(): void
  35.     {
  36.     }
  37.     #[Route('/auth/google'name'google_auth')]
  38.     public function googleLogin(Request $request)
  39.     {
  40.         $googleClient = new GoogleClient();
  41.         $googleClient->setClientId(
  42.             $this->getParameter('google')['oauth2']['credentials']['client_id']
  43.         );
  44.         $googleClient->setClientSecret(
  45.             $this->getParameter('google')['oauth2']['credentials']['client_secret']
  46.         );
  47.         $googleClient->setRedirectUri(
  48.             $this->generateUrl('google_auth', [], UrlGeneratorInterface::ABSOLUTE_URL)
  49.         );
  50.         $googleClient->addScope(['email''profile']);
  51.         if ($request->query->get('code')) {
  52.             $googleClient->fetchAccessTokenWithAuthCode($request->query->get('code'));
  53.             $googleOauth = new \Google_Service_Oauth2($googleClient);
  54.             $userInfo $googleOauth->userinfo->get();
  55.             dump($userInfo);
  56.             exit;
  57.         }
  58.         return $this->redirect($googleClient->createAuthUrl());
  59.     }
  60.     #[Route('/auth/microsoft'name'auth_microsoft')]
  61.     public function microsoftLogin(Request $requestMicrosoftLoginCheckInterface $microsoftLoginCheckUseCase): Response
  62.     {
  63.         $provider $microsoftLoginCheckUseCase->getMicrosoftProvider();
  64.         $authUrl $provider->getAuthorizationUrl();
  65.         $request->getSession()->set('oauth2state'$provider->getState());
  66.         return $this->redirect($authUrl);
  67.     }
  68.     #[Route('/auth/microsoft/check'name'auth_microsoft_check')]
  69.     public function microsoftCheck(MicrosoftLoginCheckInterface $microsoftLoginCheckUseCase): Response
  70.     {
  71.         $redirect 'root';
  72.         try {
  73.             $microsoftLoginCheckUseCase->check();
  74.             $redirect "app_home";            
  75.         } catch (GenericException $e) {
  76.             $this->addFlash('warning'$e->getMessage());
  77.         } catch (\Throwable $th) {
  78.             $this->addFlash('error'$th->getMessage());
  79.         }
  80.         return $this->redirectToRoute($redirect);
  81.     }
  82. }