# Remember me function not working

**URL:** <https://discourse.cakephp.org/t/remember-me-function-not-working/10228>\
**Category:** Plugins\
**Created:** [March 15, 2022, 8:57am UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228 "2022-03-15T08:57:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![atlet](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/atlet/32/129_2.png) [@atlet](https://discourse.cakephp.org/u/atlet)\
**Post date:** [March 15, 2022, 8:57am UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/1 "2022-03-15T08:57:20Z")

</div>

I crated a new project with CakePHP 4.2 and cakedc/users plugin (9.2). If I login and thick box remember me, cookie is crated but it’s not working properly as I get logged out in about an hour (session duration). Any hints where to check what can be wrong? I followed the installation instructions of plugin.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [March 15, 2022, 10:50am UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/2 "2022-03-15T10:50:10Z")

</div>

The default remember me functionality creates a cookie with the name `CookieAuth`

So you should have at least 3 cookies set after logging in:

- PHPSESSID
- CookieAuth
- csrfToken

If you delete the PHPSESSID cookie its like you waited 1h (or whatever your default session timeout is set therefore expiring the session)  
But for me at least the `CookieAuth` persists which has an expire duration of month and I am still logged in

---

<div class="post-metadata">

**Author:** ![atlet](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/atlet/32/129_2.png) [@atlet](https://discourse.cakephp.org/u/atlet)\
**Post date:** [March 15, 2022, 11:04am UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/3 "2022-03-15T11:04:29Z")

</div>

Yes, I have all the 3 cookies. If I delete PHPSESSID I get logged out.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [March 15, 2022, 6:22pm UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/4 "2022-03-15T18:22:16Z")

</div>

Well my test-install is on current CakePHP 4.3 and therefore latest CakeDC/Users 11 but still its basically the same logic as in 9.2

All I can say is that in `vendor/cakedc/users/src/Loader/AuthenticationServiceLoader.php` function `loadAuthenticators($service)` the authenticators are getting added in the order they are present in the Config Key `Auth.Authenticators` which should be

- Session
- Form
- Token
- Cookie
- Social
- SocialPendingEmail

See [users/users.php at 9.next · CakeDC/users · GitHub](https://github.com/CakeDC/users/blob/9.next/config/users.php#L137)

Since deleting the `PHPSESSID` Cookie therefore prevents the `SessionAuthenticator` to return a successful result the next one should be the `CookieAuthenticator` (which is overwritten by a CakeDC Custom Authenticator Class but that part shouldn’t be affected by it)

Did you overwrite some config in your `config/users.php` related to the CookieAuthenticator?

---

<div class="post-metadata">

**Author:** ![atlet](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/atlet/32/129_2.png) [@atlet](https://discourse.cakephp.org/u/atlet)\
**Post date:** [March 16, 2022, 10:13am UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/5 "2022-03-16T10:13:51Z")

</div>

> [@KevinPfeifer](#):
>
> Did you overwrite some config in your `config/users.php` related to the CookieAuthenticator?

No.

I checked and all authenticators get loaded.

---

<div class="post-metadata">

**Author:** ![atlet](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/atlet/32/129_2.png) [@atlet](https://discourse.cakephp.org/u/atlet)\
**Post date:** [March 16, 2022, 1:19pm UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/6 "2022-03-16T13:19:55Z")

</div>

I upgraded to last version of plugin (11) and to CakePHP 4.3, but still don’t work.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [March 16, 2022, 1:40pm UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/7 "2022-03-16T13:40:41Z")

</div>

Well then let me step you through where my CookieAuthenticator succeeds

As already said above it starts with the loop of all loaded authenticators in `vendor/cakedc/auth/src/Authentication/AuthenticationService.php`

In there the `$result = $authenticator->authenticate($request);` check is if the given request is valid for the used authenticator.

> <https://github.com/CakeDC/auth/blob/6.next-cake4/src/Authentication/AuthenticationService.php#L148>

When I then go into the CookieAuthenticator `vendor/cakephp/authentication/src/Authenticator/CookieAuthenticator.php` there is a call `$identity = $this->_identifier->identify(compact('username'));` which should return a valid user entity.

> <https://github.com/cakephp/authentication/blob/2.x/src/Authenticator/CookieAuthenticator.php#L108>

Also `$this->_checkToken($identity, $tokenHash)` should return true because the given token from the parsed cookie should be valid for the given entity.

> <https://github.com/cakephp/authentication/blob/2.x/src/Authenticator/CookieAuthenticator.php#L114>

Please check all these functions / return values of these functions and you may get more infos what is missing in your setup.

---

<div class="post-metadata">

**Author:** ![atlet](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/atlet/32/129_2.png) [@atlet](https://discourse.cakephp.org/u/atlet)\
**Post date:** [March 16, 2022, 2:02pm UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/8 "2022-03-16T14:02:51Z")

</div>

Tnx for great reply. I thing I found the problem. In cookie there is missing username (for login I use only email filed and in table I have username as NULL).

![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/a/a5ee2198c8f5782314b4a6db214830bacd068b0a.png)

Now I need to find solution how to set username filed with email.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [March 16, 2022, 2:15pm UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/9 "2022-03-16T14:15:22Z")

</div>

Did you do [users/Configuration.md at master · CakeDC/users · GitHub](https://github.com/CakeDC/users/blob/master/Docs/Documentation/Configuration.md#using-the-users-email-to-login)?

To be exact: Did you add the following to your config/users.php

```auto
'Auth.Identifiers.Password.fields.username' => 'email',
'Auth.Authenticators.Form.fields.username' => 'email',

```

---

<div class="post-metadata">

**Author:** ![atlet](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/atlet/32/129_2.png) [@atlet](https://discourse.cakephp.org/u/atlet)\
**Post date:** [March 16, 2022, 2:17pm UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/10 "2022-03-16T14:17:13Z")

</div>

Fixed. I just added in users.php

```auto
'Auth.Authenticators.Cookie.fields.username' => 'email',

```

And now it’s working fine. @KevinPfeifer tnx for great help!

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [March 16, 2022, 2:23pm UTC](https://discourse.cakephp.org/t/remember-me-function-not-working/10228/11 "2022-03-16T14:23:40Z")

</div>

That info about email only login would have been a good addition at the start 😉  
The default install comes with both username as well as email being a valid “username” field value.
