Not creating cookie EncryptedCookieMiddleware ,Cakephp 4

I followed documentation but no success.
I set following

$middlewareQueue->add(new CorsMiddleware($this))
            ->add(new EncryptedCookieMiddleware(
                ['CookieAuth'],
                Configure::read('Security.cookieKey')
            ))
            ->add(new AuthenticationMiddleware($this))
            ->add(new AuthorizationMiddleware($this));
 <!-- in /templates/Users/login.php -->
<div class="users form">
    <?= $this->Flash->render() ?>
    <h3>Login</h3>
    <?= $this->Form->create() ?>
    <fieldset>
        <legend><?= __('Please enter your username and password') ?></legend>
        <?= $this->Form->control('email', ['required' => true]) ?>
        <?= $this->Form->control('password', ['required' => true]) ?>
        <div class="form-group form-check">
            <input type="checkbox" class="form-check-input" id="remember-me" name="remember_me">
            <label class="form-check-label" for="remember-me">Check me out</label>
        </div>
    </fieldset>
    <?= $this->Form->submit(__('Login')); ?>
    <?= $this->Form->end() ?>
</div>

just add this code in your login functions

$cookie = [‘username’=>’’, ‘password’=>’’, ‘remember_me’=>0];
if ($this->Cookie->read(‘cq_remember_me’)) {
$cookie = $this->Cookie->read(‘cq_remember_me’);
}
Then check

// if remember_me
if($this->request->getData(‘remember_me’) == 1) {
$this->Cookie->write(‘cq_remember_me’, $this->request->getData());
}
else {
$this->Cookie->delete(‘cq_remember_me’);
}

check your ctp as my given code

<?= $this->Form->control('username', ['label'=>'Email address', 'type'=>'email', 'class'=>'form-control', 'required' => true, 'value' => $cookie['username'], 'placeholder'=>'Email','oninput'=>'this.value=this.value.toLowerCase()']); ?>
</div>
<div class="form-group">
	<?= $this->Form->control('password', ['class'=>'form-control', 'value' => $cookie['password'], 'placeholder'=>'Password']); ?>
</div>
<div class="checkbox">
	<?= $this->Form->checkbox('remember_me', ['checked' => $cookie['remember_me']]); ?> Remember Me

Tnx for your reply.
I just try to make it in middleware as in documentation cakephp 4.

Seems you’ve added the cookie middleware, but not the cookie authenticator.

1 Like

How to use cookie authenticator? There is not much things in documentation.

The AuthenticationMiddleware will call your src/Application.php getAuthenticationService() method and this is where you configure your Authentication (including installing the cookie authenticator).

This example shows how to add authenticators https://book.cakephp.org/authentication/2/en/index.html#getting-started

This diagram will give you an idea of how the AuthenticationMiddleware manages the process AuthenticationMiddleware: overview of version 2.0

Once you’ve added all the authenticators and identifiers, each authenticator will try to confirm the request by using each identifier. Once a combination succeeds, the attempts ends and the request proceeds.