1
votes

1 application. I am using ajax to log in and authenticate a user. The problem i am having is with cakephp 2.1 auth component. when I manually log in the user the component redirects which i dont want to happen but rather return a success code back to my ajax request. Is there a way to prevent this default behaviour?

1

1 Answers

2
votes

AuthComponent is calling Controller::redirect(null, 403). You can catch these by overriding redirect() in your AppController:

/**
 * Proxy for Controller::redirect() to handle AJAX redirects
 *
 * @param string $url 
 * @param int $status 
 * @param bool $exit 
 * @return void
 */
    public function redirect($url, $status = null, $exit = true) {
        // this statement catches not authenticated or not authorized ajax requests
        // AuthComponent will call Controller::redirect(null, 403) in those cases.
        // with this we're making sure that we return valid JSON responses in all cases
        if($this->request->is('ajax') && $url == null && $status == 403) {
            $this->response = new CakeResponse(array('code' => 'code'));
            $this->response->send();
            return $this->_stop();
        }
        return parent::redirect($url, $status, $exit);
    }