I've defined a route in lumen
$router->get('/', function () use ($router) {
return $router->app->version();
});
I wanted to authenticate above route using JWT Token and/or simple API Key.
I tried to use something like
$router->group(['prefix' => 'api', 'middleware' => ['auth:api', 'api_key', 'throttle']], function () use ($router) {
$router->get('/', function () use ($router) {
return $router->app->version();
});
});
api_key
is the middleware with the code
$data = $request->bearerToken();
if(empty($data))
{
return response()
->json([
'success' => false,
'status' => 401,
'message' => 'HTTP_UNAUTHORIZED'
], 401);
}
$user = User::where('api_key', $data)->first();
if(empty($user))
{
return response()
->json([
'success' => false,
'status' => 401,
'message' => 'HTTP_UNAUTHORIZED'
], 401);
}
return $next($request);
When I provide JWT Token as Authorization Bearer, it works. But when I tried to pass API Key as Authorization Bearer, it throw 401 exception from auth:api
.
How can I change the code to handle both cases in Lumen