ldc-crm/app/Http/Middleware/CheckBrand.php
2021-08-10 12:09:49 +02:00

36 lines
957 B
PHP

<?php
namespace App\Http\Middleware;
use App\Models\Service;
use Closure;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
class CheckBrand
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$apiToken = $request->bearerToken() ?? $request->api_token;
if ($apiToken === null) {
abort(401, 'Le token d\'authentification est incorrect dfd' . \json_encode($request->all()));
}
try {
$brand = Service::getFromBearerToken($apiToken)->brand;
} catch (ModelNotFoundException $e) {
abort(401, 'Le token d\'authentification est incorrect ' . $e->getMessage());
}
$request->request->add(['brand_id' => $brand->id]);
return $next($request);
}
}