So i am having some trouble with this.
I am making an authed REST api using the micro app.
In looking at the docuementation Here it looks like the easiest way to implement the auth woudl be to use the events manager and or the lifecycle before method on the $app.
Here is my before.
$app->before(function () use ($app) {
if ($app->session->get('isAuth') == false) {
$app->response->setStatusCode(401,"Unauthorized");
$app->response->setJsonContent('Unauthorized');
return false;
}
return true;
});
The problem with this is it simply returns a 200 status page with no data. It just kills the app before executing the route and theirfor and theirfor $app->response is never served. It technically works but it would make debuggin ont eh back end really annoying for someone that dint' know that "Auth" was failing.
I could do something like this. Which technically works but i hate breaking out of the return $response; method
$app->before(function () use ($app) {
if ($app->session->get('isAuth') == false) {
header('HTTP/1.0 401 Unauthorized');
header('Content-Type: application/json');
echo json_encode('Unauthorized');
return false;
}
return true;
});