@@ -44,11 +44,11 @@ and your generated code may be slightly different:
44
44
45
45
Support for login form authentication was added to ``make:auth `` in MakerBundle 1.8.
46
46
47
- This generates the following: 1) a login route & controller, 2) a template that
47
+ This generates the following: 1) login/logout routes & controller, 2) a template that
48
48
renders the login form, 3) a :doc: `Guard authenticator </security/guard_authentication >`
49
49
class that processes the login submit and 4) updates the main security config file.
50
50
51
- **Step 1. ** The ``/login `` route & controller::
51
+ **Step 1. ** The ``/login ``/`` /logout `` routes & controller::
52
52
53
53
// src/Controller/SecurityController.php
54
54
namespace App\Controller;
@@ -65,6 +65,10 @@ class that processes the login submit and 4) updates the main security config fi
65
65
*/
66
66
public function login(AuthenticationUtils $authenticationUtils): Response
67
67
{
68
+ // if ($this->getUser()) {
69
+ // return $this->redirectToRoute('target_path');
70
+ // }
71
+
68
72
// get the login error if there is one
69
73
$error = $authenticationUtils->getLastAuthenticationError();
70
74
// last username entered by the user
@@ -75,10 +79,17 @@ class that processes the login submit and 4) updates the main security config fi
75
79
'error' => $error
76
80
]);
77
81
}
82
+
83
+ /**
84
+ * @Route("/logout", name="app_logout")
85
+ */
86
+ public function logout()
87
+ {
88
+ throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
89
+ }
78
90
}
79
91
80
- Edit the ``security.yaml `` file in order to allow access for anyone to the
81
- ``/login `` route:
92
+ Edit the ``security.yaml `` file in order to declare the ``/logout `` path:
82
93
83
94
.. configuration-block ::
84
95
@@ -88,9 +99,12 @@ Edit the ``security.yaml`` file in order to allow access for anyone to the
88
99
security :
89
100
# ...
90
101
91
- access_control :
92
- - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
102
+ providers :
93
103
# ...
104
+ logout :
105
+ path : app_logout
106
+ # where to redirect after logout
107
+ # target: app_any_route
94
108
95
109
.. code-block :: xml
96
110
@@ -139,6 +153,12 @@ a traditional HTML form that submits to ``/login``:
139
153
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
140
154
{% endif %}
141
155
156
+ {% if app.user %}
157
+ <div class="mb-3">
158
+ You are logged in as {{ app.user.username }}, <a href="{{ path('app_logout') }}">Logout</a>
159
+ </div>
160
+ {% endif %}
161
+
142
162
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
143
163
<label for="inputEmail" class="sr-only">Email</label>
144
164
<input type="email" value="{{ last_username }}" name="email" id="inputEmail" class="form-control" placeholder="Email" required autofocus>
@@ -173,7 +193,6 @@ a traditional HTML form that submits to ``/login``:
173
193
174
194
use App\Entity\User;
175
195
use Doctrine\ORM\EntityManagerInterface;
176
-
177
196
use Symfony\Component\HttpFoundation\RedirectResponse;
178
197
use Symfony\Component\HttpFoundation\Request;
179
198
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -194,7 +213,7 @@ a traditional HTML form that submits to ``/login``:
194
213
{
195
214
use TargetPathTrait;
196
215
197
- private const LOGIN_ROUTE = 'app_login';
216
+ public const LOGIN_ROUTE = 'app_login';
198
217
199
218
private $entityManager;
200
219
private $urlGenerator;
@@ -252,6 +271,14 @@ a traditional HTML form that submits to ``/login``:
252
271
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
253
272
}
254
273
274
+ /**
275
+ * Used to upgrade (rehash) the user's password automatically over time.
276
+ */
277
+ public function getPassword($credentials): ?string
278
+ {
279
+ return $credentials['password'];
280
+ }
281
+
255
282
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
256
283
{
257
284
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
0 commit comments