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());
}
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.
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
In what file can I do that? And at what section?
Sorry for the ignorance but I’m new to cakedc users.
Thank you.
This is just the default config from the CakePHP Authentication plugin
'Auth' => [
'AuthenticationComponent' => [
'logoutRedirect' => '/some-url' // or https://external-domain.com
]
]
See Authentication Component - 2.x
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
How can I configure the logoutRedirect? I have it this way:
'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
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
-> % curl -I https://mydomain.com/logout
If you then get a
HTTP/1.1 302 Found
...
Location: https://mydomain.com/home
...
there is a “direct” redirect present.
But if you have
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.