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

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.

You should mention what code you used.

This is the code for logout action:

    /**
     * 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());
    }

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

1 Like

The link from @yogeshsaroya is for Cake 3 and the old Auth component. Blog Tutorial - Authentication - 4.x would be the relevant link for Cake 4.

1 Like

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

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

you can set the config for logoutRedirect