authentication done

This commit is contained in:
Olivier PARPAILLON
2024-11-19 13:07:07 +01:00
parent 51fcbba8b2
commit 8ddbf9c5d6
7 changed files with 134 additions and 34 deletions

View File

@@ -7,7 +7,7 @@ security:
# used to reload user from session & other features (e.g. switch_user) # used to reload user from session & other features (e.g. switch_user)
app_user_provider: app_user_provider:
entity: entity:
class: App\Entity\User class: App\Entity\Participant
property: email property: email
# used to reload user from session & other features (e.g. switch_user) # used to reload user from session & other features (e.g. switch_user)
# used to reload user from session & other features (e.g. switch_user) # used to reload user from session & other features (e.g. switch_user)
@@ -18,6 +18,16 @@ security:
main: main:
lazy: true lazy: true
provider: app_user_provider provider: app_user_provider
form_login:
login_path: app_login
check_path: app_login
enable_csrf: true
default_target_path: home
always_use_default_target_path: true
logout:
path: app_logout
# where to redirect after logout
# target: app_any_route
# activate different ways to authenticate # activate different ways to authenticate
# https://symfony.com/doc/current/security.html#the-firewall # https://symfony.com/doc/current/security.html#the-firewall

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Controller;
use App\Entity\Participant;
use App\Form\RegistrationFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils, Security $security, Request $request): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$user = new Participant();
$formUser = $this->createForm(RegistrationFormType::class, $user);
$formUser->handleRequest($request);
return $this->render('auth/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'formUser' => $formUser,
]);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout(Security $security): ?Response
{
// throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
return $security->logout(false);
}
}

View File

@@ -6,6 +6,7 @@ use App\Entity\Participant;
use App\Form\RegistrationFormType; use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
@@ -14,7 +15,7 @@ use Symfony\Component\Routing\Attribute\Route;
class RegistrationController extends AbstractController class RegistrationController extends AbstractController
{ {
#[Route('/register', name: 'app_register')] #[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, Security $security,): Response
{ {
$user = new Participant(); $user = new Participant();
$form = $this->createForm(RegistrationFormType::class, $user); $form = $this->createForm(RegistrationFormType::class, $user);
@@ -26,16 +27,24 @@ class RegistrationController extends AbstractController
// encode the plain password // encode the plain password
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword)); $user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));
$user->setRoles(['ROLE_USER']);
$user->setActif(false);
if ($user->getRoles() == 'ROLE_ADMIN') {
$user->setAdministrateur(true);
} else {
$user->setAdministrateur(false);
}
$entityManager->persist($user); $entityManager->persist($user);
$entityManager->flush(); $entityManager->flush();
// do anything else you need here, like send an email // do anything else you need here, like send an email
return $this->redirectToRoute('home'); // return $this->redirectToRoute('home');
return $security->login($user, 'form_login', 'main');
} }
return $this->render('registration/register.html.twig', [ return $this->render('auth/register.html.twig', [
'registrationForm' => $form, 'registrationForm' => $form,
]); ]);
} }

View File

@@ -4,13 +4,12 @@ namespace App\Form;
use App\Entity\Participant; use App\Entity\Participant;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType; use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotBlank;
@@ -28,26 +27,34 @@ class RegistrationFormType extends AbstractType
], ],
'constraints' => [ 'constraints' => [
new NotBlank([ new NotBlank([
'message' => 'Please enter a password', 'message' => 'Please enter an email address',
]), ]),
], ],
]) ])
// ->add('username', TextType::class, [ ->add('prenom', TextType::class, [
// 'label' => 'Nom d\'utilisateur', 'label' => 'Prénom',
// 'label_attr' => ['class' => 'text-gray-700 font-bold'], 'label_attr' => ['class' => 'text-gray-700 font-bold'],
// 'attr' => [ 'attr' => [
// 'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500', 'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
// 'placeholder' => 'Nom d\'utilisateur', 'placeholder' => 'Prénom',
// ], ],
// ]) ])
// ->add('agreeTerms', CheckboxType::class, [ ->add('nom', TextType::class, [
// 'mapped' => false, 'label' => 'Nom',
// 'constraints' => [ 'label_attr' => ['class' => 'text-gray-700 font-bold'],
// new IsTrue([ 'attr' => [
// 'message' => 'You should agree to our terms.', 'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
// ]), 'placeholder' => 'Nom',
// ], ],
// ]) ])
->add('telephone', IntegerType::class, [
'label' => 'Numéro de téléphone',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
'placeholder' => 'Numéro de téléphone',
],
])
->add('plainPassword', PasswordType::class, [ ->add('plainPassword', PasswordType::class, [
// instead of being set onto the object directly, // instead of being set onto the object directly,
// this is read and encoded in the controller // this is read and encoded in the controller
@@ -66,7 +73,6 @@ class RegistrationFormType extends AbstractType
new Length([ new Length([
'min' => 6, 'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters', 'minMessage' => 'Your password should be at least {{ limit }} characters',
// max length allowed by Symfony for security reasons
'max' => 4096, 'max' => 4096,
]), ]),
], ],

View File

@@ -0,0 +1,33 @@
{% extends 'main/base.html.twig' %}
{% block head %}
<head>
<meta charset="UTF-8">
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
{% endblock %}
</head>
{% endblock %}
{% block content %}
<div class="flex flex-col justify-center items-center py-48">
<div class="bg-white shadow-lg rounded-lg p-8 max-w-sm text-center">
<h2 class="text-2xl font-bold text-center pb-6">Connectez-vous !</h2>
<form action="{{ path('app_login') }}" method="post">
<label class="text-gray-700 font-bold" for="username">Email</label>
<input class="w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500" placeholder="Adresse e-mail" type="email" value="{{ last_username }}" name="_username" id="username" autocomplete="email" required autofocus>
<label class="text-gray-700 font-bold" for="password">Mot de passe</label>
<input class="w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500" placeholder="Mot de passe" type="password" name="_password" id="password" autocomplete="current-password" required>
<input type="hidden" name="_csrf_token"
value="{{ csrf_token('authenticate') }}"
>
<div class="flex flex-row justify-between">
<button class="bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 border-b-4 border-blue-700 hover:border-blue-500 rounded" type="submit">Se connecter</button>
<a href="{{ path('app_register') }}">
<button class="bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 border-b-4 border-blue-700 hover:border-blue-500 rounded mx-auto" type="button">S'inscrire</button>
</a>
</div>
</form>
</div>
</div>
{% endblock %}

View File

@@ -10,14 +10,17 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<div class="flex flex-row justify-center items-stretch py-48"> <div class="flex flex-row justify-center items-stretch py-24">
<div class="bg-gray-500 shadow-lg p-8 max-w-sm text-center flex-1"> <div class="bg-gray-500 shadow-lg p-8 max-w-sm text-center flex-1">
<h4 class="text-white text-lg font-semibold py-6">Sortir !</h4> <h2 class="text-white text-2xl font-semibold py-6">Sortir !</h2>
<p class="text-[13px] text-gray-300 mt-3 leading-relaxed">Bienvenue sur notre application web référencant tout type d'évènements à travers le monde. N'hésitez pas à vous inscrire sur notre plateforme et y invitez vos amis afins de participer à des évènements simplement et qui vous conviennent !</p> <p class="text-lg text-gray-300 mt-3 leading-relaxed">Bienvenue sur notre application web référencant tout type d'évènements à travers le monde. N'hésitez pas à vous inscrire sur notre plateforme et y invitez vos amis afins de participer à des évènements simplement et qui vous conviennent !</p>
</div> </div>
<div class="bg-white shadow-lg p-8 max-w-sm text-center flex-1"> <div class="bg-white shadow-lg p-8 max-w-sm text-center flex-1">
<h2 class="text-2xl font-bold text-center pb-6">S'inscrire</h2> <h2 class="text-2xl font-bold text-center pb-6">S'inscrire</h2>
{{ form_start(registrationForm) }} {{ form_start(registrationForm) }}
{{ form_row(registrationForm.prenom) }}
{{ form_row(registrationForm.nom) }}
{{ form_row(registrationForm.telephone) }}
{{ form_row(registrationForm.email) }} {{ form_row(registrationForm.email) }}
{{ form_row(registrationForm.plainPassword) }} {{ form_row(registrationForm.plainPassword) }}
<button class="bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 border-b-4 border-blue-700 hover:border-blue-500 rounded mx-auto" type="submit">S'inscrire</button> <button class="bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 border-b-4 border-blue-700 hover:border-blue-500 rounded mx-auto" type="submit">S'inscrire</button>

View File

@@ -27,13 +27,12 @@
<li><a href="/" class="text-gray-700 font-bold hover:text-blue-500">Sortie</a></li> <li><a href="/" class="text-gray-700 font-bold hover:text-blue-500">Sortie</a></li>
<li><a href="/" class="text-gray-700 font-bold hover:text-blue-500">ToDo</a></li> <li><a href="/" class="text-gray-700 font-bold hover:text-blue-500">ToDo</a></li>
<li><a href="/" class="text-gray-700 font-bold hover:text-blue-500">À propos</a></li> <li><a href="/" class="text-gray-700 font-bold hover:text-blue-500">À propos</a></li>
{# <li><a href="#Contact" class="text-gray-700 font-bold hover:text-blue-500">Contact</a></li>#} {% if app.user %}
{# {% if app.user %}#} <li><a href="{{ path('app_logout') }}" class="text-gray-700 font-bold hover:text-blue-500">Se déconnecter</a></li>
{# <li><a href="{{ path('auth_logout') }}" class="text-gray-700 font-bold hover:text-blue-500">Se déconnecter</a></li>#} {% else %}
{# {% else %}#} <li><a href="{{ path('app_register') }}" class="text-gray-700 font-bold hover:text-blue-500">S'inscrire</a></li>
{# <li><a href="{{ path('auth_register') }}" class="text-gray-700 font-bold hover:text-blue-500">S'inscrire</a></li>#} <li><a href="{{ path('app_login') }}" class="text-gray-700 font-bold hover:text-blue-500">Se connecter</a></li>
{# <li><a href="{{ path('auth_login') }}" class="text-gray-700 font-bold hover:text-blue-500">Se connecter</a></li>#} {% endif %}
{# {% endif %}#}
</ul> </ul>
</div> </div>
</nav> </nav>