<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
//Controller action for handling user login.
#[Route(path: '/auth-login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// Redirect authenticated users to the home page
if ($this->getUser()) {
return $this->redirectToRoute('home');
}
// Retrieve authentication error, if any
$error = $authenticationUtils->getLastAuthenticationError();
// Retrieve the last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
// Render the login template with relevant data
return $this->render('auth-login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
//Controller action for handling user logout.
#[Route(path: '/logout', name: 'app_logout')]
public function logout()
{
// Redirect to the login page after logout
return $this->redirectToRoute('app_login');
}
}