login finished.

This commit is contained in:
ASTIER Yann 2024-11-28 16:46:35 +01:00
parent 3ab2ef5a9e
commit f38c5e4350
6 changed files with 53 additions and 33 deletions

View File

@ -1,7 +1,7 @@
security: security:
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers: password_hashers:
Symfony\Component\Security\Core\Employee: 'auto' Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers: providers:
# used to reload user from session & other features (e.g. switch_user) # used to reload user from session & other features (e.g. switch_user)
@ -40,7 +40,7 @@ security:
# Easy way to control access for large sections of your site # Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used # Note: Only the *first* access control that matches will be used
access_control: access_control:
#- { path: ^/*, roles: ROLE_USER } - { path: ^/*, roles: ROLE_USER }
when@test: when@test:
security: security:

View File

@ -9,7 +9,7 @@ use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class DashboardController extends AbstractController class DashboardController extends AbstractController
{ {
#[Route(path: '/dashboard', name: 'app_dashboard')] #[Route(path: '/dashboard', name: 'dashboard')]
public function index(): Response public function index(): Response
{ {
return $this->render('dashboard/index.html.twig'); return $this->render('dashboard/index.html.twig');

View File

@ -2,7 +2,9 @@
namespace App\Controller; namespace App\Controller;
use App\Form\LoginType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
@ -13,12 +15,13 @@ class SecurityController extends AbstractController
#[Route(path: '', name: '_login')] #[Route(path: '', name: '_login')]
public function login(AuthenticationUtils $authenticationUtils): Response public function login(AuthenticationUtils $authenticationUtils): Response
{ {
// if ($this->getUser()) { // if ($this->getUser()) {
// return $this->redirectToRoute('target_path'); // return $this->redirectToRoute('dashboard');
// } // }
// get the login error if there is one // get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError(); $error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user // last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername(); $lastUsername = $authenticationUtils->getLastUsername();
@ -26,7 +29,7 @@ class SecurityController extends AbstractController
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]); return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
} }
#[Route(path: '/logout', name: 'app_logout')] #[Route(path: '/logout', name: '_logout')]
public function logout(): void public function logout(): void
{ {
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.'); throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');

View File

@ -6,16 +6,17 @@ use App\Repository\MissionCategoryRepository;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: MissionCategoryRepository::class)] #[ORM\Entity(repositoryClass: MissionCategoryRepository::class)]
#[ORM\UniqueConstraint(columns: ['mission', 'category'])]
class MissionCategory class MissionCategory
{ {
#[ORM\Id] #[ORM\Id]
#[ORM\OneToMany(targetEntity: Mission::class, mappedBy: 'MissionCategory')] #[ORM\ManyToOne(targetEntity: Mission::class, inversedBy: 'missionCategories')]
#[ORM\JoinColumn(nullable: false)] #[ORM\Column(type: 'integer')]
private ?Mission $mission = null; private ?Mission $mission = null;
#[ORM\Id] #[ORM\Id]
#[ORM\OneToMany(targetEntity: Category::class, mappedBy: 'MissionCategory')] #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'missionCategories')]
#[ORM\JoinColumn(nullable: false)] #[ORM\Column(type: 'integer')]
private ?Category $category = null; private ?Category $category = null;
public function getMission(): ?Mission public function getMission(): ?Mission

View File

@ -20,7 +20,7 @@ class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
{ {
use TargetPathTrait; use TargetPathTrait;
public const LOGIN_ROUTE = 'app_login'; public const LOGIN_ROUTE = 'security_login';
public function __construct(private UrlGeneratorInterface $urlGenerator) public function __construct(private UrlGeneratorInterface $urlGenerator)
{ {
@ -28,13 +28,13 @@ class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
public function authenticate(Request $request): Passport public function authenticate(Request $request): Passport
{ {
$email = $request->getPayload()->getString('email'); $email = $request->getPayload()->getString('_username');
$request->getSession()->set(SecurityRequestAttributes::LAST_USERNAME, $email); $request->getSession()->set(SecurityRequestAttributes::LAST_USERNAME, $email);
return new Passport( return new Passport(
new UserBadge($email), new UserBadge($email),
new PasswordCredentials($request->getPayload()->getString('password')), new PasswordCredentials($request->getPayload()->getString('_password')),
[ [
new CsrfTokenBadge('authenticate', $request->getPayload()->getString('_csrf_token')), new CsrfTokenBadge('authenticate', $request->getPayload()->getString('_csrf_token')),
new RememberMeBadge(), new RememberMeBadge(),
@ -44,12 +44,12 @@ class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{ {
if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) { /*if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
return new RedirectResponse($targetPath); return new RedirectResponse($targetPath);
} }*/
// For example: // For example:
return new RedirectResponse($this->urlGenerator->generate('app_dashboard')); return new RedirectResponse($this->urlGenerator->generate('dashboard'));
//return new RedirectResponse($this->urlGenerator->generate('DashboardController')); //return new RedirectResponse($this->urlGenerator->generate('DashboardController'));
} }

View File

@ -3,23 +3,39 @@
{% block title %}HegreLand{% endblock %} {% block title %}HegreLand{% endblock %}
{% block body %} {% block body %}
<form method="post"> <form method="post">
{% if error %} {% if error %}
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div> <div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %} {% endif %}
{% if app.user %} {% if app.user %}
<div class="mb-3"> <div class="mb-3">
Vous êtes déjà connecté(e) en tant que {{ app.user.userIdentifier }}, <a href="{{ path('app_logout') }}">Me déconnecter</a> You are logged in as {{ app.user.userIdentifier }}, <a href="{{ path('security_logout') }}">Logout</a>
</div>
{% endif %}
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
<label for="username">Email</label>
<input type="email" value="{{ last_username }}" name="_username" id="username" class="form-control" autocomplete="email" required autofocus>
<label for="password">Password</label>
<input type="password" name="_password" id="password" class="form-control" autocomplete="current-password" required>
<input type="hidden" name="_csrf_token"
value="{{ csrf_token('authenticate') }}"
>
{#
Uncomment this section and add a remember_me option below your firewall to activate remember me functionality.
See https://symfony.com/doc/current/security/remember_me.html
<div class="checkbox mb-3">
<input type="checkbox" name="_remember_me" id="_remember_me">
<label for="_remember_me">Remember me</label>
</div> </div>
{% endif %} #}
<button class="btn btn-lg btn-primary" type="submit">
<h1 class="h3 mb-3 font-weight-normal">Me connecter</h1> Sign in
{{ form_row(loginForm.email) }} </button>
<button class="btn btn-lg btn-primary" type="submit"> </form>
Se connecter
</button>
<p><a href="#"> Mot de passe oublié</a> </p>
</form>
{% endblock %} {% endblock %}