src/Controller/SecurityController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\UserRepository;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  10. class SecurityController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/login", name="app_login")
  14.      */
  15.     public function login(AuthenticationUtils $authenticationUtils): Response
  16.     {
  17.          if ($this->getUser()) {
  18.              return $this->redirectToRoute('default');
  19.          }
  20.         // get the login error if there is one
  21.         $error $authenticationUtils->getLastAuthenticationError();
  22.         // last username entered by the user
  23.         $lastUsername $authenticationUtils->getLastUsername();
  24.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  25.     }
  26.     /**
  27.      * @Route("/login_check", name="login_check")
  28.      */
  29.     public function check()
  30.     {
  31.         throw new \LogicException('This code should never be reached');
  32.     }
  33.     /**
  34.      * @Route("/login/link", name="login_link")
  35.      */
  36.     public function requestLoginLink(LoginLinkHandlerInterface $loginLinkHandlerUserRepository $userRepositoryRequest $request)
  37.     {
  38.         $loginLink "";
  39.         if ($request->isMethod('POST')) {
  40.             // load the user in some way (e.g. using the form input)
  41.             $email = (array)json_decode($request->getContent());
  42.             $user $userRepository->findOneBy(['email' => $email['email']]);
  43.             // create a login link for $user this returns an instance
  44.             // of LoginLinkDetails
  45.             $loginLinkDetails $loginLinkHandler->createLoginLink($user);
  46.             $loginLink $loginLinkDetails->getUrl();
  47.             // ... send the link and return a response (see next section)
  48.         }
  49.         $response = new Response(json_encode(["link"=>$loginLink]));
  50.         $response->headers->set('Content-Type''*');
  51.         // Allow all websites
  52.         $response->headers->set('Access-Control-Allow-Origin''*');
  53.         // Or a predefined website
  54.         return $response;
  55.     }
  56.     /**
  57.      * @Route("/logout", name="app_logout")
  58.      */
  59.     public function logout()
  60.     {
  61.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  62.     }
  63. }