You probably noticed that sometimes an Ajax request will return the login page instead of the actual content is should return. This happens when the user has beed logged out in the background and the current page does not reflect that (it could happen if the session expired or if the user simply logged out from another browser window/tab).
How to Fix Symfony2 Ajax Login Redirect
Here’s a quick way to fix this: we will create an event listener that will catch this authentication exception, check for an Ajax request and, if found, it will return a 403 http code instead of redirecting to the login page. The JavaScript code will then know to reload the page and thus redirect to login in case of 403 instead of loading and showing the received content to the user.
Here’s the Symfony2 event listener:
<?php // src/AppBundle/EventListener/AjaxAuthenticationListener.php namespace AppBundle\EventListener; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; /** */ class AjaxAuthenticationListener { /** * Handles security related exceptions. * * @param GetResponseForExceptionEvent $event An GetResponseForExceptionEvent instance */ public function onCoreException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $request = $event->getRequest(); if ($request->isXmlHttpRequest()) { if ($exception instanceof AuthenticationException || $exception instanceof AccessDeniedException) { $event->setResponse(new Response('', 403)); } } } }
As always, we will have to register it as a service:
services: ajax.authentication.listener: class: AppBundle\EventListener\AjaxAuthenticationListener tags: - { name: kernel.event_listener, event: kernel.exception, method: onCoreException, priority: 1000 }
How to Fix Symfony2 Ajax Login Redirect
In the JavaScript code we add the following to make jQuery treat the Ajax errors by reloading the window in case of a 403 error. What will actually happen is that the user will end on the login page as he is no longer authenticated.
$(document).ready(function() { $(document).ajaxError(function (event, jqXHR) { if (403 === jqXHR.status) { window.location.reload(); } }); });
17 Comments
Comments are closed.
The alternative is to upgrade to the latest Symfony version. As of 2.6 or 2.7 (I don’t remember when we added it), requests detected as being AJAX ones (through the X-Requested-With header) are not redirected to the login page anymore.
Hi Christophe! I just tested with Symfony 2.7. An AJAX call to a secured action redirected and returned the login page. Could you point me to the place where I can find more details about the feature you described above? Thanks!
Thx a lot for this
Hey I know tthis is off topic but I was wondering if you knew of any
widgets I could add to my blog that automatically
twet my newest twitter updates. I’ve been looking for a plug-in like this for quite soke tie and was hoping maybe you would
have soe experience wth something like this. Pleease let me know if
you run into anything. I truly enjoy reading yoir blog and I
look forward to your new updates.
Very nice article, just what I was looking for.
It’s remarkable for me to have a site, which iss helpful for
my knowledge. thankms admin
It’s actually a cool and helpful piecfe of info. I am satisfied that you simply shareed this useful information with us.
Please keep us informed like this. Thanks for sharing.
Good article! We aare linking tto this particularly great article
on our site. Keep uup the good writing.
Hi, I do think this is a great blog. I stumbledupon it 😉 I’m going too come back yet again since
i have book marked it. Money and freedom is the greatest way
to change, may yoou be rich andd continue to help other people.
Hello to every one, it’s actually a nice for me to pay a visit this web site,
it includes important Information.
Hi! Would you mind if I share your blog with my twitter group?
There’s a loot of folks that I think would really enjoy your content.
Please let me know. Thank you
Hi there! This article couldn’t be written much better!
Looking at this article reminds me of my previous roommate!
He continually kept talking about this. I am goiing to forward this article tto him.
Fairly certain he’ll have a great read. Many tthanks for sharing!
Hi there, I enjoy reading through your article. I like to
write a little comment to support you.
Nice post. I learn something new and challenging on websites I stumbleupon everyday.
It will always be exciting to read through articles from oother aauthors
annd use a little something from their web sites.
Wow, superb blog layout! How long have you been blogging for?
you made blogging lok easy. The oveall look of your website
is fantastic, as well as the content!
Heya i’m for the first time here. I found this board and I find
It trjly useful & it helped me out much. I hope to give something back and help others like you aided
me.
I am realy thankful to the holder of this webste who has shared thus great paragraph aat
at this place.