Fix Auth service provider to return apiAuthFailed exception on api auth fail
This commit is contained in:
parent
a3b919b1c5
commit
6edb0e34db
1 changed files with 13 additions and 9 deletions
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Enums\ApiError;
|
||||
use App\Exceptions\Api\V2\ApiErrorException;
|
||||
use App\Lib\Helper;
|
||||
use App\Models\Service;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
|
@ -37,7 +39,7 @@ class AuthServiceProvider extends ServiceProvider
|
|||
$apiToken = $request->api_token;
|
||||
|
||||
if (is_null($apiToken)) {
|
||||
return null;
|
||||
throw new ApiErrorException(ApiError::apiAuthFailed(), 401);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -47,20 +49,22 @@ class AuthServiceProvider extends ServiceProvider
|
|||
** this to route as a parameter
|
||||
*/
|
||||
if (!($route = Helper::getRoute($request))) {
|
||||
return null;
|
||||
throw new ApiErrorException(ApiError::apiAuthFailed(), 401);
|
||||
}
|
||||
|
||||
try {
|
||||
$service = Service::getFromApiToken($apiToken);
|
||||
$service = $service->load('brand');
|
||||
app()->bind(Service::class, function () use ($service) {
|
||||
return $service;
|
||||
});
|
||||
$test = $service->authenticateApi($apiToken);
|
||||
return $test ? $service : null;
|
||||
} catch (ModelNotFoundException $e) {
|
||||
return null;
|
||||
throw new ApiErrorException(ApiError::apiAuthFailed(), 401);
|
||||
}
|
||||
$service = $service->load('brand');
|
||||
app()->bind(Service::class, function () use ($service) {
|
||||
return $service;
|
||||
});
|
||||
if ($service->authenticateApi($apiToken)) {
|
||||
return $service;
|
||||
}
|
||||
throw new ApiErrorException(ApiError::apiAuthFailed(), 401);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue