@@ -16,7 +16,7 @@ enable new users to register.
1616Use composer to install the Authentication Plugin:
1717
1818``` bash
19- composer require " cakephp/authentication:~ 4.0"
19+ composer require " cakephp/authentication:^ 4.0"
2020```
2121
2222## Adding Password Hashing
@@ -45,7 +45,7 @@ Because we want to hash the password each time it is set, we'll use a mutator/se
4545CakePHP will call a convention based setter method any time a property is set in one of your
4646entities. 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
5050namespace App\Model\Entity;
5151
@@ -109,7 +109,7 @@ In **src/Application.php**, add the following imports:
109109use Authentication\AuthenticationService;
110110use Authentication\AuthenticationServiceInterface;
111111use Authentication\AuthenticationServiceProviderInterface;
112- use Authentication\Identifier\AbstractIdentifier ;
112+ use Authentication\Identifier\PasswordIdentifier ;
113113use Authentication\Middleware\AuthenticationMiddleware;
114114use 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
132130public 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
147147public 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,23 +175,20 @@ 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
192189In your ` AppController ` class add the following code:
193190
194- ``` php {7 }
191+ ``` php {8 }
195192// src/Controller/AppController.php
196193public function initialize(): void
197194{
@@ -232,9 +229,8 @@ If you visit your site, you'll get an "infinite redirect loop" so let's fix that
232229
233230In your ` UsersController ` , add the following code:
234231
235- ::: code-group
236-
237- ``` php [UsersController.php]
232+ ``` php
233+ // src/Controller/UsersController.php
238234public 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-
282279Now the login page will allow us to correctly login into the application.
283280Test it by requesting any page of your site. After being redirected
284281to the ` /users/login ` page, enter the email and password you
@@ -291,7 +288,7 @@ We need to add a couple more details to configure our application.
291288We want all ` view ` and ` index ` pages accessible without logging in so we'll add this specific
292289configuration in AppController:
293290
294- ``` php {6 }
291+ ``` php {7 }
295292// in src/Controller/AppController.php
296293public function beforeFilter(\Cake\Event\EventInterface $event): void
297294{
@@ -336,7 +333,7 @@ If you try to visit **/users/add** without being logged in, you will be
336333redirected to the login page. We should fix that as we want to allow people to
337334sign up for our application. In the ` UsersController ` update the ` beforeFilter ` :
338335
339- ``` php {3 }
336+ ``` php {2 }
340337// In UsersController::beforeFilter()
341338$this->Authentication->allowUnauthenticated(['login', 'add']);
342339```
0 commit comments