@@ -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
@@ -137,6 +151,12 @@ a traditional HTML form that submits to ``/login``:
137
151
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
138
152
{% endif %}
139
153
154
+ {% if app.user %}
155
+ <div class="mb-3">
156
+ You are logged in as {{ app.user.username }}, <a href="{{ path('app_logout') }}">Logout</a>
157
+ </div>
158
+ {% endif %}
159
+
140
160
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
141
161
<label for="inputEmail" class="sr-only">Email</label>
142
162
<input type="email" value="{{ last_username }}" name="email" id="inputEmail" class="form-control" placeholder="Email" required autofocus>
@@ -171,7 +191,6 @@ a traditional HTML form that submits to ``/login``:
171
191
172
192
use App\Entity\User;
173
193
use Doctrine\ORM\EntityManagerInterface;
174
-
175
194
use Symfony\Component\HttpFoundation\RedirectResponse;
176
195
use Symfony\Component\HttpFoundation\Request;
177
196
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -192,7 +211,7 @@ a traditional HTML form that submits to ``/login``:
192
211
{
193
212
use TargetPathTrait;
194
213
195
- private const LOGIN_ROUTE = 'app_login';
214
+ public const LOGIN_ROUTE = 'app_login';
196
215
197
216
private $entityManager;
198
217
private $urlGenerator;
@@ -250,6 +269,14 @@ a traditional HTML form that submits to ``/login``:
250
269
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
251
270
}
252
271
272
+ /**
273
+ * Used to upgrade (rehash) the user's password automatically over time.
274
+ */
275
+ public function getPassword($credentials): ?string
276
+ {
277
+ return $credentials['password'];
278
+ }
279
+
253
280
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
254
281
{
255
282
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
0 commit comments