src/Controller/SecurityController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use ApiPlatform\Core\Serializer\JsonEncoder;
  4. use App\Entity\User;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. class SecurityController extends AbstractController
  12. {
  13.     #[Route(path'/login'name'app_login')]
  14.     public function login(AuthenticationUtils $authenticationUtils): Response
  15.     {
  16.         // if ($this->getUser()) {
  17.         //     return $this->redirectToRoute('target_path');
  18.         // }
  19.         // get the login error if there is one
  20.         $error $authenticationUtils->getLastAuthenticationError();
  21.         // last username entered by the user
  22.         $lastUsername $authenticationUtils->getLastUsername();
  23.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  24.     }
  25.     #[Route(path'/logout'name'app_logout')]
  26.     public function logout(): never
  27.     {
  28.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  29.     }
  30.     #[Route('/api/login'name'api_login'methods: ['POST'])]
  31.     public function jsonLogin() : Response
  32.     {
  33.         /** @var User $user */
  34.         $user $this->getUser();
  35.         if ($user) {
  36.             return $this->json([
  37.                 'id' => $user->getId(),
  38.                 'username' => $user->getUsername(),
  39.                 'name' => $user->getName() . ' ' $user->getLastname(),
  40.                 'profile' => $user->getProfile(),
  41.                 'image' => $user->getImage(),
  42.             ]);
  43.         } else {
  44.             return $this->json([], 500);
  45.         }
  46.     }
  47.     #[Route('/api/account'name'api_account')]
  48.     public function accountApi(): JsonResponse
  49.     {
  50.         $user $this->getUser();
  51.         return $this->json($user200, [], [
  52.                 'groups' => ['main'],
  53.             ]);
  54.     }
  55. }