Auth logout is not working in CakePHP 2.x

别等时光非礼了梦想. 提交于 2019-12-12 16:18:07

问题


  1. When I login from one user account session is set.Then opening the next tab on same browser and enter login url it takes me to the login page.But actually it should redirect to the "dashboard" page(in my case). It can't redirect to loginRedirect(dashboard) page as mentioned in my Auth.
  2. When i logout, as per my code session,cookie and cache are deleted. but it's not redirect to logoutRedirect page.

My code :

App controller

public $components = array(
  'Session', 'RequestHandler', 'Email', 'Cookie',
  'Auth' => array(
    'authenticate' => array(
      'Form' => array(
        'fields' => array(
          'username' => 'email',
          'password' => 'password')
        )
      ),
      'loginRedirect' => array(
         'controller' => 'users',
         'action' => 'login'
       ),
       'logoutRedirect' => array(
         'controller' => 'users',
         'action' => 'login'
       )
     )
   );

User controller

login action :

public function login() {
  $this->layout = 'admin';    
    if ($this->Session->check('Auth.User')) {
      $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));      
    }
    if (isset($this->data['User'])) {
      if (!empty($this->data['User']['email']) && !empty($this->data['User']['password'])) {
        if ($this->Auth->login()) {   
          $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
        } else {
          $this->set('error', "Email or Password mismatch.");
        }
      }
    } else {
      if ($this->Auth->loggedIn()) {                
        $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
      }
    }
  } 

logout action :

public function logout() {      
  header('pragma: no-cache'); 
  header('Cache-Control: no-cache, must-revalidate'); 
  $this->response->disableCache();        
  $this->Session->delete('Auth.User');
  $this->Session->delete('User');
  $this->Session->destroy();
  $this->Cookie->destroy();
  return $this->redirect($this->Auth->logout());
}

This code is working fine in "local server" but not working in production server.


回答1:


Your redirect statements should have return in front of them so that code execution will stop there. For example:

return $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));


来源:https://stackoverflow.com/questions/33776887/auth-logout-is-not-working-in-cakephp-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!