Skip to content

Commit 21cc781

Browse files
authored
Merge pull request #8225 from cakephp/fix-auth-tutorial-v4-corrections
Fix Authentication plugin v4 tutorial corrections
2 parents 4537922 + 8dc3721 commit 21cc781

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

docs/en/tutorials-and-examples/cms/authentication.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Because we want to hash the password each time it is set, we'll use a mutator/se
4545
CakePHP will call a convention based setter method any time a property is set in one of your
4646
entities. Let's add a setter for the password in **src/Model/Entity/User.php**:
4747

48-
```php {3,12-18}
48+
```php {4,12-18}
4949
<?php
5050
namespace App\Model\Entity;
5151

@@ -109,7 +109,7 @@ In **src/Application.php**, add the following imports:
109109
use Authentication\AuthenticationService;
110110
use Authentication\AuthenticationServiceInterface;
111111
use Authentication\AuthenticationServiceProviderInterface;
112-
use Authentication\Identifier\AbstractIdentifier;
112+
use Authentication\Identifier\PasswordIdentifier;
113113
use Authentication\Middleware\AuthenticationMiddleware;
114114
use Psr\Http\Message\ServerRequestInterface;
115115
```
@@ -123,11 +123,9 @@ class Application extends BaseApplication
123123
{
124124
```
125125

126-
Then add the following methods:
126+
Then add the following to your `middleware()` method:
127127

128-
::: code-group
129-
130-
```php [middleware()]
128+
```php
131129
// src/Application.php
132130
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
133131
{
@@ -142,7 +140,9 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
142140
}
143141
```
144142

145-
```php [getAuthenticationService()]
143+
Next, add the `getAuthenticationService()` method:
144+
145+
```php
146146
// src/Application.php
147147
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
148148
{
@@ -160,8 +160,8 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
160160
]);
161161

162162
$fields = [
163-
AbstractIdentifier::CREDENTIAL_USERNAME => 'email',
164-
AbstractIdentifier::CREDENTIAL_PASSWORD => 'password',
163+
PasswordIdentifier::CREDENTIAL_USERNAME => 'email',
164+
PasswordIdentifier::CREDENTIAL_PASSWORD => 'password',
165165
];
166166

167167
// Load the authenticators. Session should be first.
@@ -175,18 +175,15 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
175175
'action' => 'login',
176176
],
177177
'identifier' => [
178-
'Authentication.Password' => [
179-
'fields' => $fields,
180-
],
178+
'className' => 'Authentication.Password',
179+
'fields' => $fields,
181180
],
182181
]);
183182

184183
return $service;
185184
}
186185
```
187186

188-
:::
189-
190187
### Configuring the AppController
191188

192189
In your `AppController` class add the following code:
@@ -232,9 +229,8 @@ If you visit your site, you'll get an "infinite redirect loop" so let's fix that
232229

233230
In your `UsersController`, add the following code:
234231

235-
::: code-group
236-
237-
```php [UsersController.php]
232+
```php
233+
// src/Controller/UsersController.php
238234
public function beforeFilter(\Cake\Event\EventInterface $event): void
239235
{
240236
parent::beforeFilter($event);
@@ -260,7 +256,10 @@ public function login()
260256
}
261257
```
262258

263-
```php [templates/Users/login.php]
259+
Next, add the template for your login action:
260+
261+
```php
262+
<!-- templates/Users/login.php -->
264263
<div class="users form content">
265264
<?= $this->Flash->render() ?>
266265
<h3>Login</h3>
@@ -277,8 +276,6 @@ public function login()
277276
</div>
278277
```
279278

280-
:::
281-
282279
Now the login page will allow us to correctly login into the application.
283280
Test it by requesting any page of your site. After being redirected
284281
to the `/users/login` page, enter the email and password you

0 commit comments

Comments
 (0)