Here's how I've solved the issue:

Server-side:
If the user tries to access a page and their session has expired, or are not
authenticated, I return a 401 status code, then forward to the login page,
which I think would look like this:

$this->getResponse()->setHttpResponseCode(401);
$this->_forward('login', 'auth');

In the login action, you can then decide how to render the login form
depending on the type of request. eg, you could either return a full login
page, or just the login form itself if ajax. If you output the full page,
you can extract the form itself from the ajax response (using jQuery):
var loginForm = $('#login-form', request.responseText).html();

Client-side:
I'm using jQuery, but this should give you the general gist of it:
$.ajaxSetup({
    error: function(request, status, errorThrown){
        if(request.status == 401){
            //Open a dialog box and inject the login form
            dialog(request.responseText, 'Login Required', 250, 500,
{primary: 'Log In'});
        }
    }
});

Lastly, if you're wanting to redirect instead of forward, you can still work
off the status code, but you'll need to then inspect the xhr object and look
for a Location header, which you can then use to either pass the user on to
the login page, or load the login form into a dialog box.

Cheers,
David
-- 
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/Ajax-Sessions-and-Redirects-tp3241713p3243565.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to