laravel / sanctum

Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.
https://laravel.com/docs/sanctum
MIT License
2.76k stars 296 forks source link

Getting error 401 sometimes when there is a lot of request in short period of time #541

Closed AlexKratky closed 1 day ago

AlexKratky commented 2 days ago

Sanctum Version

4.0.3

Laravel Version

11.28.1

PHP Version

8.2.12

Database Driver & Version

No response

Description

Sometimes, when I send more requests in short period of time (talking about 3 to 10 requests per seconds), I sometimes receive error 401, even though that token is presented, its same as request before or after (and the request before or after passes). I did not find any info about this behavior, so I assuming its some kind of bug. I do have any throttling and even if there is some hidden, the error would be 429. I also tried switching to cookie authentication, but the problem is still the same.

Snímek obrazovky 2024-11-12 193254 Snímek obrazovky 2024-11-12 193310 Snímek obrazovky 2024-11-12 193316 Snímek obrazovky 2024-11-12 193324 Snímek obrazovky 2024-11-12 204142

Steps To Reproduce

just call any route under sanctum, for this scenario I use simple test:

setTimeout(() => { for (let i = 0; i <= 50; i++) { setTimeout(() => fetch(...), 50 * i) }

},5000)

AlexKratky commented 1 day ago

Okay, I figured it out, it seems the problem is because laravel fails to properly load non-cached configuration, so running php artisan config:cache solves this problem. Of course that the cache command should be used on production and not on local env. It would be nice to somehow handle this, but I found that other libs has similar issue but not so often, so its bug with laravel itself.