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

62 lines
1.7 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;
use Illuminate\Auth\AuthenticationException;
2020-12-01 17:02:10 +01:00
use Illuminate\Auth\Middleware\Authenticate as Middleware;
2021-03-14 10:45:42 +01:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Route;
2020-12-01 17:02:10 +01:00
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)
{
$isApiV1Request = $request->isJson() && Helper::isRequestForApiV1($request);
// TODO: check this works
if ($isApiV1Request) {
// Use patched v1 Auth mecanism (without bearer reimplementation)
$bearerToken = $request->bearerToken();
$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');
}
}
}