Closed pamungkasandono closed 2 months ago
My guess is you would do
if (!$token) {
echo Flight::json([
"message" => "Token required!."
], 400);
return false; // add this line.
}
Thank you for your response.
When I tried using return false
the return response, what I received is:
Forbidden
Do you have any other suggestions?
Instead of return false, try exit();
Worked for me, redirects were acting strange.
Ahh yeah, like @krmu mentioned, I would either directly use exit;
to stop execution.
@pamungkasandono I've updated the documentation to be more clear about what behavior to expect. https://docs.flightphp.com/learn/middleware#handling-middleware-errors
@krmu my middleware is now working as expected with the addition of exit();
. @n0nag0n documenting this would be really helpful for other. Thanks, everyone! I truly appreciate it!
Thinking more about this along with some other things that have surfaced, I came up with this PR. https://github.com/flightphp/core/pull/594 Would love your feedback.
So in this case you would just do
Flight::jsonHalt([
"message" => "Token required!."
], 400);
No exit
, no Flight::halt()
method. It would immediately stop execution on that line.
When I try to validate the token in the middleware, the output generated is not as expected. Here's an example of the middleware I created:
If I don't send the token in the header, the expected output should be:
But in this case, why is the output:
Could you please assist me? Is there something missing?