From 680a0841b84fbd0ef750502798314e915e6d92f3 Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 29 Dec 2021 02:00:02 +0100 Subject: [PATCH] Add form login screen --- config/packages/security.yaml | 10 ++++-- src/Controller/LoginController.php | 33 +++++++++++++++++++ src/Controller/RegistrationController.php | 2 +- src/Controller/TestController.php | 10 ++++-- templates/base/base.html.twig | 10 +++--- templates/registration/register.html.twig | 17 ---------- templates/security/login.html.twig | 40 +++++++++++++++++++++++ templates/security/register.html.twig | 15 +++++++++ 8 files changed, 111 insertions(+), 26 deletions(-) create mode 100644 src/Controller/LoginController.php delete mode 100644 templates/registration/register.html.twig create mode 100644 templates/security/login.html.twig create mode 100644 templates/security/register.html.twig diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 7aa253b..4554f63 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -18,8 +18,14 @@ security: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: - lazy: true +# lazy: true provider: app_user_provider + form_login: + login_path: login + check_path: login + enable_csrf: true + logout: + path: logout # activate different ways to authenticate # https://symfony.com/doc/current/security.html#the-firewall @@ -30,7 +36,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: ^/admin, roles: ROLE_ADMIN } + - { path: ^/admin, roles: ROLE_ADMIN } # - { path: ^/profile, roles: ROLE_USER } when@test: diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php new file mode 100644 index 0000000..862ddc2 --- /dev/null +++ b/src/Controller/LoginController.php @@ -0,0 +1,33 @@ +getUser()) { + // return $this->redirectToRoute('target_path'); + // } + + // get the login error if there is one + $error = $authenticationUtils->getLastAuthenticationError(); + // last username entered by the user + $lastUsername = $authenticationUtils->getLastUsername(); + + return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]); + } + + #[Route('/logout', name: 'logout')] + public function logout() + { + throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.'); + } +} diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index f78773a..6487beb 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -36,7 +36,7 @@ class RegistrationController extends AbstractController return $this->redirectToRoute('app_test_test1'); } - return $this->render('registration/register.html.twig', [ + return $this->render('security/register.html.twig', [ 'registrationForm' => $form->createView(), ]); } diff --git a/src/Controller/TestController.php b/src/Controller/TestController.php index ae4ba36..d9f32eb 100644 --- a/src/Controller/TestController.php +++ b/src/Controller/TestController.php @@ -5,12 +5,18 @@ namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; -#[Route('/test')] +//#[Route('/test')] class TestController extends AbstractController { - #[Route('/1')] + #[Route('/test/1')] public function test1() { return $this->render('base/base.html.twig'); } + + #[Route('/')] + public function home() + { + return $this->render('base/base.html.twig'); + } } \ No newline at end of file diff --git a/templates/base/base.html.twig b/templates/base/base.html.twig index 180f8d4..5d41353 100644 --- a/templates/base/base.html.twig +++ b/templates/base/base.html.twig @@ -12,21 +12,23 @@ integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> - {{ encore_entry_link_tags('app') }} +{# {{ encore_entry_link_tags('app') }}#} {% endblock %} {% include 'base/navbar.html.twig' %}
- {% block body %} - {% endblock %} +
+ {% block body %} + {% endblock %} +
{% block javascripts %} - {{ encore_entry_script_tags('app') }} +{# {{ encore_entry_script_tags('app') }}#} {% endblock %} diff --git a/templates/registration/register.html.twig b/templates/registration/register.html.twig deleted file mode 100644 index 667f5ac..0000000 --- a/templates/registration/register.html.twig +++ /dev/null @@ -1,17 +0,0 @@ -{% extends 'base/base.html.twig' %} - -{% block body %} -
-

Register

- - {{ form_start(registrationForm) }} - {{ form_row(registrationForm.username) }} - {{ form_row(registrationForm.plainPassword, { - label: 'Password' - }) }} - {{ form_row(registrationForm.agreeTerms) }} - - - {{ form_end(registrationForm) }} -
-{% endblock %} diff --git a/templates/security/login.html.twig b/templates/security/login.html.twig new file mode 100644 index 0000000..08d2534 --- /dev/null +++ b/templates/security/login.html.twig @@ -0,0 +1,40 @@ +{% extends 'base/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.username }}, 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 %} \ No newline at end of file diff --git a/templates/security/register.html.twig b/templates/security/register.html.twig new file mode 100644 index 0000000..1f8256d --- /dev/null +++ b/templates/security/register.html.twig @@ -0,0 +1,15 @@ +{% extends 'base/base.html.twig' %} + +{% block body %} +

Register

+ + {{ form_start(registrationForm) }} + {{ form_row(registrationForm.username) }} + {{ form_row(registrationForm.plainPassword, { + label: 'Password' + }) }} + {{ form_row(registrationForm.agreeTerms) }} + + + {{ form_end(registrationForm) }} +{% endblock %}