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

57 lines
1.6 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Http\Middleware;
2021-03-14 10:45:42 +01:00
use App\Lib\Helper;
use App\Models\Service;
use Closure;
2020-12-01 17:02:10 +01:00
use Illuminate\Auth\Middleware\Authenticate as Middleware;
2021-03-14 10:45:42 +01:00
/**
* Updated for ApiV2
*/
2020-12-01 17:02:10 +01:00
class Authenticate extends Middleware
{
2021-03-14 10:45:42 +01:00
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string[] ...$guards
* @return mixed
*
* @throws \Illuminate\Auth\AuthenticationException
*/
public function handle($request, Closure $next, ...$guards)
{
// TODO: check this works
2021-08-10 10:20:52 +02:00
if (Helper::isRequestForApiV1($request)) {
2021-03-14 10:45:42 +01:00
// Use patched v1 Auth mecanism (without bearer reimplementation)
2021-08-16 18:47:31 +02:00
$bearerToken = Helper::getBearerTokenFromRequest($request) ?? $request->api_token;
2021-03-14 10:45:42 +01:00
$brand = Service::getFromBearerToken($bearerToken)->brand;
$request->request->add(['brand_id' => $brand->id]);
$response = $next($request);
} else {
// Use Laravel auth check mecanism
$response = \call_user_func_array(
[$this, 'parent::' . __FUNCTION__],
array_merge([$request, $next], $guards)
);
}
return $response;
}
2020-12-01 17:02:10 +01:00
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
}