62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Auth\Middleware\Authenticate as Middleware;
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
/**
|
|
* Updated for ApiV2
|
|
*/
|
|
class Authenticate extends Middleware
|
|
{
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param mixed $request
|
|
* @param \Closure $next
|
|
* @param mixed ...$guards
|
|
* @return mixed
|
|
*
|
|
* @throws \Illuminate\Auth\AuthenticationException If authentification fails.
|
|
*/
|
|
public function handle(mixed $request, Closure $next, mixed ...$guards)
|
|
{
|
|
// Use Laravel auth check mecanism.
|
|
return \call_user_func_array(
|
|
[$this, 'parent::' . __FUNCTION__],
|
|
array_merge([$request, $next], $guards)
|
|
);
|
|
}
|
|
/**
|
|
* Get the path the user should be redirected to when they are not authenticated.
|
|
*
|
|
* @param mixed $request
|
|
* @return string|null
|
|
* @codeCoverageIgnore
|
|
*/
|
|
protected function redirectTo(mixed $request)
|
|
{
|
|
$logins = [
|
|
'backend' => route('login')
|
|
];
|
|
if (!$request->expectsJson()) {
|
|
// * Notify must login
|
|
Session::flash('warning', trans('Veuillez vous connecter pour continuer'));
|
|
|
|
foreach ($request->route()->gatherMiddleware() as $middleware) {
|
|
if (\is_string($middleware) and strpos($middleware, 'auth:') !== false) {
|
|
$route = $logins[str_replace('auth:', '', $middleware)] ?? null;
|
|
if ($route) {
|
|
return $route;
|
|
}
|
|
}
|
|
}
|
|
return route('login');
|
|
}
|
|
abort(401);
|
|
|
|
return null;
|
|
}
|
|
}
|