Why don't you use Laravel's form validation with Laravel Precognition for this? https://laravel.com/docs/11.x/precognition
API returns text/html code 200 instead of JSON with error
Dear artisans, I'm looking for your experience with Inertia, for a JSON issue I can't fix properly. Here is the problematic page on GitHub (https://github.com/hrsa/calendize/blob/master/resources/js/Pages/Auth/Login.vue).
I do an API call to the controller (https://github.com/hrsa/calendize/blob/master/app/Http/Controllers/UserController.php) to check if user's email exists and it works well - until, in a day or so, it doesn't. Nothing changes in the code, but the response starts coming back as text/html, thus messing up the logic. What's really frustrating is that this behavior is random - the app may run 1,2,3 days - and suddenly goes into "text/html" mode when calling the API. What's more, i've dumped the controller response - and it's JSON, as one expects. So it gets transformed somewhere... but where? The code seems logical and fine, what 🐘 in the room am I missing here?
I think i got it right this time! Let me share the process with those who'll read this thread - and maybe it'll help someone sometime.
The culprit wasn't nginx, because the issue came back. I asked for help everywhere i could (with no reaction whatsoever), and started examining the system again.
The server went into text/html mode randomly - sometimes in an hour after deployment, sometimes - in a day or two. But it was never longer than 2 days.
When getting those pesky text/html responses, i noticed the increased response time - it grew from 200-300 ms to 950-1500 ms. That got me thinking - if nothing else is happening on the server, why does it become so slow all of a sudden?
So i ssh'd to the server, and started looking at the load. Memory usage seemed OK, but i noticed that the CPU was loaded at 93%! After 10 minutes, CPU load didn't decrease at all, so i started looking for a reason why the usage was so high - using top to find out that the php container was using all that power and then - using docker top php to see that there were some strange processes /tmp/kinsing and /tmp/kdevtmpfsi running for a long time and using CPU.
Then, with some googling, it wasn't hard to figure out that it was a crypto-miner malware the crept it through the port 9000 which was exposed by mistake. Fortunately, it ONLY invaded the docker container - so a quick rebuild was enough to get rid of it instantly. After properly sanitizing the exposed ports - i saw a multitude of incoming connections trying to start a "debugging" session on my previously exposed 9000 port - so yeah, that finally was the logical answer to this random bug!
The moral of the story is simple - be sure to never expose more ports than needed in your docker configuration and your server! I hope this helps someone who struggles too!
Please or to participate in this conversation.