I'm having a problem with my slim app, I'm trying to use JsonWebToken for authentication but I don't know how to do it the right way.
My middleware is blocking all the requests that dont include a valid token, but what about the first authentication post request that obviously don't include a valid token. Here's my code if it helps (in middleware file):
$app->add(function (Request $request,Response $response, $next) use ($app){
$stringToken = $request->getHeader("Authorization")[0];
if($stringToken == NULL) {
return $response->withJson(array("Connection"=>"Fail On Token", "Error"=>"No token Provided."));
} else {
$jsonObjectToken = json_decode($stringToken);
try{
JWT::decode($jsonObjectToken->jwt, JWTController::$secretKey, array('HS512'));
}catch (Exception $e){
return $response->withJson(array("Connection"=>"Fail On Token", "Error"=>$e->getMessage()));
}
$response = $next($request, $response);
return $response;
}
});