From 2faae9e088d5712b39e1d3368a29dd2b45786ae1 Mon Sep 17 00:00:00 2001 From: bastien ollier Date: Wed, 22 May 2024 17:30:31 +0200 Subject: [PATCH] Connexion par formulaire --- config/packages/security.yaml | 8 +++++ src/Controller/LoginController.php | 27 +++++++++++++++++ src/Controller/SecurityController.php | 32 ++++++++++++++++++++ templates/login/index.html.twig | 22 ++++++++++++++ templates/security/login.html.twig | 42 +++++++++++++++++++++++++++ 5 files changed, 131 insertions(+) create mode 100644 src/Controller/LoginController.php create mode 100644 src/Controller/SecurityController.php create mode 100644 templates/login/index.html.twig create mode 100644 templates/security/login.html.twig diff --git a/config/packages/security.yaml b/config/packages/security.yaml index fbfb8ed..405c27d 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -16,6 +16,14 @@ security: main: lazy: true provider: app_user_provider + form_login: + login_path: app_login + check_path: app_login + enable_csrf: true + logout: + path: app_logout + # where to redirect after logout + # target: app_any_route # activate different ways to authenticate # https://symfony.com/doc/current/security.html#the-firewall diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php new file mode 100644 index 0000000..ba8f478 --- /dev/null +++ b/src/Controller/LoginController.php @@ -0,0 +1,27 @@ +getLastAuthenticationError(); + + // last username entered by the user + $lastUsername = $authenticationUtils->getLastUsername(); + + return $this->render('login/index.html.twig', [ + 'last_username' => $lastUsername, + 'error' => $error, + ]); + + } +} diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php new file mode 100644 index 0000000..76bf5c4 --- /dev/null +++ b/src/Controller/SecurityController.php @@ -0,0 +1,32 @@ +getLastAuthenticationError(); + + // last username entered by the user + $lastUsername = $authenticationUtils->getLastUsername(); + + return $this->render('security/login.html.twig', [ + 'last_username' => $lastUsername, + 'error' => $error, + ]); + } + + #[Route(path: '/logout', name: 'app_logout')] + public function logout(): void + { + throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.'); + } +} diff --git a/templates/login/index.html.twig b/templates/login/index.html.twig new file mode 100644 index 0000000..9bdb835 --- /dev/null +++ b/templates/login/index.html.twig @@ -0,0 +1,22 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello LoginController!{% endblock %} + +{% block body %} + {% if error %} +
{{ error.messageKey|trans(error.messageData, 'security') }}
+ {% endif %} + +
+ + + + + + + {# If you want to control the URL the user is redirected to on success + #} + + +
+{% endblock %} diff --git a/templates/security/login.html.twig b/templates/security/login.html.twig new file mode 100644 index 0000000..916c1d3 --- /dev/null +++ b/templates/security/login.html.twig @@ -0,0 +1,42 @@ +{% extends 'base.html.twig' %} + +{% block title %}Log in!{% endblock %} + +{% block body %} +
+ {% if error %} +
{{ error.messageKey|trans(error.messageData, 'security') }}
+ {% endif %} + + {% if app.user %} +
+ You are logged in as {{ app.user.userIdentifier }}, Logout +
+ {% endif %} + +

Please sign in

+ + + + + + + + {# + 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 + +
+ +
+ #} + + +
+{% endblock %}