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

38 lines
997 B
PHP
Raw Permalink Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Http\Middleware;
2021-08-16 18:47:31 +02:00
use App\Lib\Helper;
2020-12-01 17:02:10 +01:00
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-16 18:47:31 +02:00
$apiToken = Helper::getBearerTokenFromRequest($request) ?? $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);
}
}