ldc-op-insta/app/Http/Middleware/Authenticate.php

41 lines
1.1 KiB
PHP
Raw Normal View History

2023-02-28 12:27:16 +01:00
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
2023-04-21 14:49:53 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
2023-02-28 12:27:16 +01:00
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
2023-04-21 14:49:53 +00:00
protected function redirectTo(Request $request): ?string
2023-02-28 12:27:16 +01:00
{
2023-04-21 14:49:53 +00: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;
}
}
}
2023-02-28 12:27:16 +01:00
return route('login');
}
2023-04-21 14:49:53 +00:00
abort(401);
return null;
2023-02-28 12:27:16 +01:00
}
}