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:
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
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
providers:
# 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
# Note: Only the *first* access control that matches will be used
access_control:
#- { path: ^/*, roles: ROLE_USER }
- { path: ^/*, roles: ROLE_USER }
when@test:
security:

View File

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

View File

@ -2,7 +2,9 @@
namespace App\Controller;
use App\Form\LoginType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
@ -13,12 +15,13 @@ class SecurityController extends AbstractController
#[Route(path: '', name: '_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// if ($this->getUser()) {
// return $this->redirectToRoute('dashboard');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
@ -26,7 +29,7 @@ class SecurityController extends AbstractController
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
{
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;
#[ORM\Entity(repositoryClass: MissionCategoryRepository::class)]
#[ORM\UniqueConstraint(columns: ['mission', 'category'])]
class MissionCategory
{
#[ORM\Id]
#[ORM\OneToMany(targetEntity: Mission::class, mappedBy: 'MissionCategory')]
#[ORM\JoinColumn(nullable: false)]
#[ORM\ManyToOne(targetEntity: Mission::class, inversedBy: 'missionCategories')]
#[ORM\Column(type: 'integer')]
private ?Mission $mission = null;
#[ORM\Id]
#[ORM\OneToMany(targetEntity: Category::class, mappedBy: 'MissionCategory')]
#[ORM\JoinColumn(nullable: false)]
#[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'missionCategories')]
#[ORM\Column(type: 'integer')]
private ?Category $category = null;
public function getMission(): ?Mission

View File

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

View File

@ -3,23 +3,39 @@
{% block title %}HegreLand{% endblock %}
{% block body %}
<form method="post">
{% if error %}
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form method="post">
{% if error %}
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
{% if app.user %}
<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>
{% if app.user %}
<div class="mb-3">
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>
{% endif %}
#}
<h1 class="h3 mb-3 font-weight-normal">Me connecter</h1>
{{ form_row(loginForm.email) }}
<button class="btn btn-lg btn-primary" type="submit">
Se connecter
</button>
<p><a href="#"> Mot de passe oublié</a> </p>
</form>
<button class="btn btn-lg btn-primary" type="submit">
Sign in
</button>
</form>
{% endblock %}