add some more details on api key error

This commit is contained in:
Nabeel Shahzad
2017-12-30 11:58:34 -06:00
parent 2bfaa9e584
commit aba90ea2d9

View File

@@ -22,14 +22,14 @@ class ApiAuth
{ {
// Check if Authorization header is in place // Check if Authorization header is in place
if(!$request->header('Authorization')) { if(!$request->header('Authorization')) {
return $this->unauthorized(); return $this->unauthorized('Authorization header missing');
} }
// Try to find the user via API key. Cache this lookup // Try to find the user via API key. Cache this lookup
$api_key = $request->header('Authorization'); $api_key = $request->header('Authorization');
$user = User::where('api_key', $api_key)->first(); $user = User::where('api_key', $api_key)->first();
if($user === null) { if($user === null) {
return $this->unauthorized(); return $this->unauthorized('User not found with key "'.$api_key.'"');
} }
// Set the user to the request // Set the user to the request
@@ -46,13 +46,13 @@ class ApiAuth
* Return an unauthorized message * Return an unauthorized message
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
*/ */
private function unauthorized() private function unauthorized($details='')
{ {
return response([ return response([
'error' => [ 'error' => [
'code' => '401', 'code' => '401',
'http_code' => 'Unauthorized', 'http_code' => 'Unauthorized',
'message' => 'Invalid or missing API key', 'message' => 'Invalid or missing API key ('. $details .')',
], ],
], 401); ], 401);
} }