You’ll need to start with some change to your authentication setup, to return something (e.g. a different HTTP status code) instead of redirecting on an authentication error for Ajax calls. Exactly how to do that part would very much depend on many details of your current implementation, but that’s the general gist of it.
Then, the load method can accept a callback parameter. I’m no jQuery expert, so can’t say for sure, but I think that you could catch the status in that function and redirect the page at that point.
After some time dealing with this problem, it happens that the solution I gave does not work most of the time.
First, the request does not always return an error (in fact, most of the time not).
Second, when doing this this, you cannot catch another error and, if needed, cannot track any other problem.
So I ended to do that : (in UsersController → login)
if ($this->request->is('ajax')) {
return $this->redirect($this->referer());
}
Redirecting to the referer ends up loading the login page if you are no longer connected.
I’ve been doing this for more than a week now and had no problem.
Thank you for contributing.
The solution I gave has been working in production applications for some time now and works perfectly.
Bur I agree I should move to axios now