ldc-crm/app/Http/Middleware/CheckBrand.php

37 lines
957 B
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Http\Middleware;
use App\Models\Service;
use Closure;
2021-08-10 10:20:52 +02:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
2020-12-01 17:02:10 +01:00
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)
{
2021-08-10 10:20:52 +02:00
$apiToken = $request->bearerToken() ?? $request->api_token;
2020-12-01 17:02:10 +01:00
2021-08-10 10:20:52 +02:00
if ($apiToken === null) {
abort(401, 'Le token d\'authentification est incorrect dfd' . \json_encode($request->all()));
}
2020-12-01 17:02:10 +01:00
2021-08-10 10:20:52 +02:00
try {
$brand = Service::getFromBearerToken($apiToken)->brand;
} catch (ModelNotFoundException $e) {
abort(401, 'Le token d\'authentification est incorrect ' . $e->getMessage());
}
2020-12-01 17:02:10 +01:00
$request->request->add(['brand_id' => $brand->id]);
return $next($request);
}
}