Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions docs/en/tutorials-and-examples/cms/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Because we want to hash the password each time it is set, we'll use a mutator/se
CakePHP will call a convention based setter method any time a property is set in one of your
entities. Let's add a setter for the password in **src/Model/Entity/User.php**:

```php {3,12-18}
```php {4,12-18}
<?php
namespace App\Model\Entity;

Expand Down Expand Up @@ -109,7 +109,7 @@ In **src/Application.php**, add the following imports:
use Authentication\AuthenticationService;
use Authentication\AuthenticationServiceInterface;
use Authentication\AuthenticationServiceProviderInterface;
use Authentication\Identifier\AbstractIdentifier;
use Authentication\Identifier\PasswordIdentifier;
use Authentication\Middleware\AuthenticationMiddleware;
use Psr\Http\Message\ServerRequestInterface;
```
Expand All @@ -123,11 +123,9 @@ class Application extends BaseApplication
{
```

Then add the following methods:
Then add the following to your `middleware()` method:

::: code-group

```php [middleware()]
```php
// src/Application.php
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
Expand All @@ -142,7 +140,9 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
}
```

```php [getAuthenticationService()]
Next, add the `getAuthenticationService()` method:

```php
// src/Application.php
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
Expand All @@ -160,8 +160,8 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
]);

$fields = [
AbstractIdentifier::CREDENTIAL_USERNAME => 'email',
AbstractIdentifier::CREDENTIAL_PASSWORD => 'password',
PasswordIdentifier::CREDENTIAL_USERNAME => 'email',
PasswordIdentifier::CREDENTIAL_PASSWORD => 'password',
];

// Load the authenticators. Session should be first.
Expand All @@ -175,18 +175,15 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe
'action' => 'login',
],
'identifier' => [
'Authentication.Password' => [
'fields' => $fields,
],
'className' => 'Authentication.Password',
'fields' => $fields,
],
]);

return $service;
}
```

:::

### Configuring the AppController

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

In your `UsersController`, add the following code:

::: code-group

```php [UsersController.php]
```php
// src/Controller/UsersController.php
public function beforeFilter(\Cake\Event\EventInterface $event): void
{
parent::beforeFilter($event);
Expand All @@ -260,7 +256,10 @@ public function login()
}
```

```php [templates/Users/login.php]
Next, add the template for your login action:

```php
<!-- templates/Users/login.php -->
<div class="users form content">
<?= $this->Flash->render() ?>
<h3>Login</h3>
Expand All @@ -277,8 +276,6 @@ public function login()
</div>
```

:::

Now the login page will allow us to correctly login into the application.
Test it by requesting any page of your site. After being redirected
to the `/users/login` page, enter the email and password you
Expand Down