简体   繁体   中英

CakePHP: on login, redirecting to last page viewed before logout

I've implemented Authentication and Authorization in Cake, and mostly it works as needed. However, if I hit log out from a page that requires particular credentials (say /admin) and log back in as another differently privileged user, I get redirected to /admin and an error message displayed.

Looking at the request headers in chrome, I notice that the Cookie CAKEPHP is still set even after log out.

public function login() {
    ...
        if ($this->Auth->login()) {
              $this->set('login_failed', false);
        return $this->redirect($this->Auth->redirect());
    } else {
    ...
    }
}


public function logout() {
    return $this->redirect($this->Auth->logout());
}

Any ideas about how I can troubleshoot this?

assign this to your AppController

public $components = array(               
        'Auth'=> array(                
            'logoutRedirect' => array('controller' => 'users', 'action'=>'login'),
            'loginRedirect' => array('controller' => 'users', 'action' => 'login')
          )
)

your logout function also is incorrect. You should do this:

public function logout(){
       $this->Auth->logout();                
       $this->redirect($this->Auth->loginAction);
}

loginRedirect

logoutRedirect

Not sure which version of cake you are using, but you can try to unset the cookie and that should keep it from going to the last viewed page. You can add it to logout or to beforefilter().

It probably depends on the CakePHP version.

But you could try:

$this->Session->delete('Auth.redirect');

Which is working on CakePHP 2.x

Is admin a prefix? If so, try setting admin to false for the logout link in the admin view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM