ldc-nature-and-respect/app/Http/Middleware/Authenticate.php

38 lines
1 KiB
PHP
Raw Normal View History

2022-11-18 12:39:03 +01:00
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
2023-03-15 10:31:55 +01:00
use Illuminate\Support\Facades\Session;
2022-11-18 12:39:03 +01:00
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
2023-03-15 10:31:55 +01:00
* @param mixed $request
2022-11-18 12:39:03 +01:00
* @return string|null
*/
protected function redirectTo($request)
{
2023-03-15 10:31:55 +01:00
$logins = [
'backend' => route('bo.login')
];
if (!$request->expectsJson()) {
// * Notify must login
2023-03-16 15:08:37 +01:00
Session::flash('warning', trans('Please login to continue'));
2023-03-15 10:31:55 +01:00
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-11-18 12:39:03 +01:00
return route('login');
}
2023-03-15 10:31:55 +01:00
abort(401);
2022-11-18 12:39:03 +01:00
}
}