Tuesday, 4 October 2011

Ajax redirects with devise

When devise times out, the user should be forced to log in again. This works fine when the user attempts to access my app using an html request, but ajax requests were bringing up the http_authentication pop-up box from the browser. Googling around suggested various ways of dealing with this, all of which seemed to involve a lot of unnecessary work. Just putting
// Set up a global AJAX error handler to handle the 401
// unauthorized responses. If a 401 status code comes back,
// the user is no longer logged-into the system and can not
// use it properly.
$.ajaxSetup({
statusCode: {
401: function(){

// Redirect the to the login page.
location.href = "/";;

}
}
});
in the application.js file solved the problem.