Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auth::check() fails on an ajax call (sometimes)

I have a logged user that access my JavaScript app.

During the initialization, the app send a couple of Ajax calls to gather some informations.

Sometimes, I would say about one time out of ten, one of the calls abort in one of my route filters.

What I observed about it :

  • doesn't occurs every time
  • not always the same route (call)
  • there can be more than one fail at a same time
  • a simple page refresh re-trigger the calls, and as it's not a constant failure, everything goes back to normal... until the next glitch.

Here's the filter that is faulty:
I know it's this one because I replaced the 403 with 418 and it transformed the "forbidden" glitch into a "teapot" glitch.

Route::filter('auth-api', function() {
   if (!Auth::check()) { App::abort(403, "Auth-api filter denied"); } 
});

And here's the strange bug in action :
All the /api/[whatever] goes though the same filters, in this case, the /api/assurances died while the others went good.

enter image description here

like image 213
Pascal Boutin Avatar asked Nov 10 '22 18:11

Pascal Boutin


1 Answers

It sounds like your sessions are failing for some reason. It is possibly due to the file session driver, which can lead to race conditions when accessed multiple times in short succession.

The best option is to change the session driver and test if the problem persists with another session driver. I recommend trying with Redis or Memcache - as these are designed to be fast, quick, and reliable.

like image 137
Laurence Avatar answered Nov 15 '22 12:11

Laurence