User::init:
$this->loginRequiredAjaxResponse = json_encode(array('ajaxError'=>array('code'=>401,'redirect'=>$this->loginUrl)));User::login (snip, I added headers)
elseif(isset($this->loginRequiredAjaxResponse))
{
header('HTTP/1.1 401 Unauthorized');
header('Content-type: application/json');
echo $this->loginRequiredAjaxResponse;
Yii::app()->end();
}We can then have the following in our JS to catch timeouts / required logins. This will hook on before our more specialized handlers (if any):
$.ajaxSetup({
global: true,
error:function(jqXHR, textStatus, errorThrown){
var localJson = eval('('+jqXHR.responseText+')');
if(!localJson.ajaxError || typeof localJson.ajaxError == 'undefined')
{
return;
}
if(typeof localJson.ajaxError.redirect == 'string')
{
window.location.replace(localJson.ajaxError.redirect);
}
}
});This is especially usefull when we are loading ajax-tabs etc which earlier returned the actual login page.
To be able to hook on to .error we need to set a header that's >= 300.
My suggestion is either to have CUser::loginRequiredAjaxResponse as string|function, or just make it a protected function which people can override. It's not a huge change, but it's way more convenient.
EDIT: Tracked at Google Code

Help













