From 5900454ad7a053eb75dd94c0822c5cb91f8c1374 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 13 Feb 2022 09:38:28 +0100 Subject: [PATCH] Add 15.4: Configuring the Security Authentication --- config/packages/security.yaml | 5 +++ src/Controller/SecurityController.php | 36 ++++++++++++++++ src/Security/AppAuthenticator.php | 59 +++++++++++++++++++++++++++ templates/security/login.html.twig | 42 +++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 src/Controller/SecurityController.php create mode 100644 src/Security/AppAuthenticator.php create mode 100644 templates/security/login.html.twig diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 92a38d9..2476fcc 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -20,6 +20,11 @@ security: main: lazy: true provider: app_user_provider + custom_authenticator: App\Security\AppAuthenticator + 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/SecurityController.php b/src/Controller/SecurityController.php new file mode 100644 index 0000000..65c096f --- /dev/null +++ b/src/Controller/SecurityController.php @@ -0,0 +1,36 @@ +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="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/src/Security/AppAuthenticator.php b/src/Security/AppAuthenticator.php new file mode 100644 index 0000000..101bc2d --- /dev/null +++ b/src/Security/AppAuthenticator.php @@ -0,0 +1,59 @@ +urlGenerator = $urlGenerator; + } + + public function authenticate(Request $request): Passport + { + $username = $request->request->get('username', ''); + + $request->getSession()->set(Security::LAST_USERNAME, $username); + + return new Passport( + new UserBadge($username), + new PasswordCredentials($request->request->get('password', '')), + [ + new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')), + ] + ); + } + + public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response + { + if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) { + return new RedirectResponse($targetPath); + } + + return new RedirectResponse($this->urlGenerator->generate('admin')); + } + + protected function getLoginUrl(Request $request): string + { + return $this->urlGenerator->generate(self::LOGIN_ROUTE); + } +} diff --git a/templates/security/login.html.twig b/templates/security/login.html.twig new file mode 100644 index 0000000..fbaba11 --- /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.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 %}