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

39 lines
1.2 KiB
PHP
Raw Normal View History

2022-05-20 11:54:44 +02:00
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
2022-06-01 13:41:24 +02:00
use Illuminate\Support\Facades\Session;
2022-05-20 11:54:44 +02:00
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
2022-06-01 13:41:24 +02:00
* @param \Illuminate\Http\Request $request
2022-05-20 11:54:44 +02:00
* @return string|null
2022-06-01 13:41:24 +02:00
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
2022-05-20 11:54:44 +02:00
*/
protected function redirectTo($request)
{
2022-06-01 13:41:24 +02:00
$logins = [
'backend' => route('bo.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;
}
}
}
2022-07-04 10:17:34 +00:00
return collect($logins)->first() ?? '/';
2022-05-20 11:54:44 +02:00
}
2022-06-01 13:41:24 +02:00
abort(401);
2022-05-20 11:54:44 +02:00
}
}