# Redirect to login action after logout with cakedc users in cakephp 4.2

**URL:** <https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379>\
**Category:** Need Help\
**Created:** [May 7, 2022, 4:51pm UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379 "2022-05-07T16:51:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [May 7, 2022, 4:51pm UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/1 "2022-05-07T16:51:33Z")

</div>

How can I make the site to redirect to login action when I click on logout? When I do that it redirects me to controller Home action index and it allows me to go to the different links in the site.

---

<div class="post-metadata">

**Author:** ![yogeshsaroya](https://avatars.discourse-cdn.com/v4/letter/y/f05b48/32.png) [@yogeshsaroya](https://discourse.cakephp.org/u/yogeshsaroya)\
**Post date:** [May 8, 2022, 9:05am UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/2 "2022-05-08T09:05:50Z")

</div>

You should mention what code you used.

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [May 8, 2022, 3:59pm UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/3 "2022-05-08T15:59:05Z")

</div>

This is the code for logout action:

```auto
    /**
     * Logout
     *
     * @return mixed
     */
    public function logout()
    {
        $user = $this->getRequest()->getAttribute('identity');
        $user = $user ?? [];

        $eventBefore = $this->dispatchEvent(Plugin::EVENT_BEFORE_LOGOUT, ['user' => $user]);
        if (is_array($eventBefore->getResult())) {
            return $this->redirect($eventBefore->getResult());
        }

        $this->getRequest()->getSession()->destroy();
        $this->Flash->success(__d('cake_d_c/users', 'You\'ve successfully logged out'));

        $eventAfter = $this->dispatchEvent(Plugin::EVENT_AFTER_LOGOUT, ['user' => $user]);
        if (is_array($eventAfter->getResult())) {
            return $this->redirect($eventAfter->getResult());
        }

        return $this->redirect($this->Authentication->logout());
    }
```

---

<div class="post-metadata">

**Author:** ![yogeshsaroya](https://avatars.discourse-cdn.com/v4/letter/y/f05b48/32.png) [@yogeshsaroya](https://discourse.cakephp.org/u/yogeshsaroya)\
**Post date:** [May 8, 2022, 4:02pm UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/4 "2022-05-08T16:02:56Z")

</div>

> [@mdeanquin](#):
>
> `$this->Authentication->logout()`

[https://book.cakephp.org/3/en/tutorials-and-examples/blog-auth-example/auth.html#authentication-login-and-logout](https://book.cakephp.org/3/en/tutorials-and-examples/blog-auth-example/auth.html#authentication-login-and-logout)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 8, 2022, 11:52pm UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/5 "2022-05-08T23:52:17Z")

</div>

The link from @yogeshsaroya is for Cake 3 and the old Auth component. [Blog Tutorial - Authentication - 4.x](https://book.cakephp.org/4/en/tutorials-and-examples/blog-auth-example/auth.html#logout) would be the relevant link for Cake 4.

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [May 11, 2022, 1:38am UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/6 "2022-05-11T01:38:31Z")

</div>

Thank you, but I want to know with cakedc users plugin not authentication plugin

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 11, 2022, 1:44am UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/7 "2022-05-11T01:44:38Z")

</div>

Are you aware that the CakeDC users plugin uses the CakePHP authentication and authorization plugins?

---

<div class="post-metadata">

**Author:** ![thomasg](https://avatars.discourse-cdn.com/v4/letter/t/9d8465/32.png) [@thomasg](https://discourse.cakephp.org/u/thomasg)\
**Post date:** [May 14, 2022, 7:37am UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/8 "2022-05-14T07:37:58Z")

</div>

you can set the config for logoutRedirect

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [May 29, 2022, 3:11pm UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/9 "2022-05-29T15:11:29Z")

</div>

In what file can I do that? And at what section?  
Sorry for the ignorance but I’m new to cakedc users.  
Thank you.

---

<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:** [May 29, 2022, 5:50pm UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/10 "2022-05-29T17:50:09Z")

</div>

This is just the default config from the CakePHP Authentication plugin

```auto
    'Auth' => [
        'AuthenticationComponent' => [
            'logoutRedirect' => '/some-url' // or https://external-domain.com
        ]
    ]

```

See [Authentication Component - 2.x](https://book.cakephp.org/authentication/2/en/authentication-component.html)

Just so you remember: CakeDC/Users builds upon the CakePHP Authentication and Authorization Plugins. If you can’t find a specific config in the CakeDC/Users Documentation then try to find it in the official CakePHP documentation

---

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [May 29, 2022, 7:12pm UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/11 "2022-05-29T19:12:38Z")

</div>

How can I configure the logoutRedirect? I have it this way:

```auto
    'Auth' => [
        'Authentication' => [
            'serviceLoader' => \CakeDC\Users\Loader\AuthenticationServiceLoader::class
        ],
        'AuthenticationComponent' => [
            'load' => true,
            'loginRedirect' => '/',
            'logoutRedirect' => ['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'login'],
            'requireIdentity' => false
        ],

```

and is still redirecting to /home

---

<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:** [May 29, 2022, 7:44pm UTC](https://discourse.cakephp.org/t/redirect-to-login-action-after-logout-with-cakedc-users-in-cakephp-4-2/10379/12 "2022-05-29T19:44:17Z")

</div>

defining it that way works for me so if you still get a redirect

- your browser caches an old redirect you had in there (which chrome likes to do) or
- you have some sort of redirect for the login action present

You can sort of debug redirects via curl like so

```auto
-> % curl -I https://mydomain.com/logout                                        

```

If you then get a

```auto
HTTP/1.1 302 Found
...
Location: https://mydomain.com/home
...

```

there is a “direct” redirect present.

But if you have

```auto
HTTP/1.1 302 Found
...
Location: https://mydomain.com/users/login
...

```

then its at least redirecting correctly.

If you are not on Linux or MacOS I can’t tell you how to do that in your OS, I just know how it works with curl.
