gerl
gerl

Reputation: 1181

cakephp - auth logout redirect

In cakephp2.0, I have domain.com but when I logout (url is domain.com/logout), it is redirected to domain.com/app/login. I dont want it to redirect to domain.com/app/login but instead redirect to domain.com/logout. These are the codes I have in my UsersController & my AppController

class AppController extends Controller {
public $helpers = array ('Html', 'Form', 'Js' => array('Jquery'), 'Session');

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
         'authorize' => array('Controller') // Added this line
    )
);
}

Userscontroller

class UsersController extends AppController {
public $components = array(

    'Auth' => array(
        'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
         'authorize' => array('Controller') // Added this line
    )
);

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'logout', 'login');
    $this->Auth->deny('view');
}

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {

            $this->redirect('http://domain.com/thankyou');
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

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

Any help would be great. Thanks.

Upvotes: 0

Views: 17093

Answers (3)

gerl
gerl

Reputation: 1181

I ended up using this in my logout() function

$this->Auth->logout();
$this->redirect(some url);

Upvotes: 7

Elium1984
Elium1984

Reputation: 154

Did you check that ?

'Auth' => array(
    'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
    'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
)

And

public function logout() {
    //$this->redirect($this->Auth->logout());
    //Change for :
    //I suppose that you have a logout.ctp view in your View/Pages
    $this->redirect(array('controller' => 'pages', 'action' => 'display', 'logout')
}

Then in your rootes

Router::connect('/logout', array('controller' => 'pages', 'action' => 'display', 'logout'));

of course don't forget

$this->Auth->allow('display'

Upvotes: 1

Bill Rollins
Bill Rollins

Reputation: 1756

Do you have a view for the logout page? Something that you are trying to display after they are logged out? What might be happening is that the user is logged out, but Cake is unable to display the logout page because it is secure, so Cake redirects to the login page.

If you have security enabled and a page you want to be displayed to a user that is not logged in, you will need to include something like this in its controller:

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('login','logout');
}

Upvotes: 2

Related Questions