src/Controller/SecurityController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  4. use KnpU\OAuth2ClientBundle\Client\Provider\GoogleClient;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  10. class SecurityController extends AbstractController
  11. {
  12.     #[Route(path'/login'name'app_login')]
  13.     public function login(AuthenticationUtils $authenticationUtils): Response
  14.     {
  15.         if ($this->getUser()) {
  16.             return $this->redirectToRoute('app_home');
  17.         }
  18.         // get the login error if there is one
  19.         $error $authenticationUtils->getLastAuthenticationError();
  20.         // last username entered by the user
  21.         $lastUsername $authenticationUtils->getLastUsername();
  22.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  23.     }
  24.     #[Route(path'/connect/google'name'google_connect')]
  25.     public function connectGoogle(ClientRegistry $clientRegistry): RedirectResponse
  26.     {
  27.         /**
  28.          * @var GoogleClient $client
  29.          */
  30.         $client $clientRegistry->getClient('google');
  31.         return $client->redirect(['https://www.googleapis.com/auth/userinfo.email''https://www.googleapis.com/auth/userinfo.profile']);
  32.     }
  33.     #[Route(path'/connect/github'name'github_connect')]
  34.     public function connectGithub(ClientRegistry $clientRegistry): RedirectResponse
  35.     {
  36.         /**
  37.          * @var GithubClient $client
  38.          */
  39.         $client $clientRegistry->getClient('github');
  40.         return $client->redirect(['read:user','user:email']);
  41.     }
  42.     #[Route(path'/connect/facebook'name'facebook_connect')]
  43.     public function connectFacebook(ClientRegistry $clientRegistry): RedirectResponse
  44.     {
  45.         /**
  46.          * @var FacebookClient $client
  47.          */
  48.         $client $clientRegistry->getClient('facebook');
  49.         return $client->redirect(['public_profile''email']);
  50.     }
  51.     #[Route(path'/logout'name'app_logout')]
  52.     public function logout(): void
  53.     {
  54.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  55.     }
  56. }