fix: 🐛 Middleware blocca solo le routes GET

This commit is contained in:
Maicol Battistini 2022-01-18 11:35:38 +01:00
parent 36f7299e8b
commit 2f7c341317
No known key found for this signature in database
GPG Key ID: 4FDB0F87CDB1D34A
1 changed files with 15 additions and 4 deletions

View File

@ -4,6 +4,7 @@ namespace App\Http\Middleware;
use App\Models\User;
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
@ -11,16 +12,26 @@ use Illuminate\Support\Facades\DB;
class CheckConfigurationMiddleware
{
public function handle(Request $request, Closure $next): Response|RedirectResponse
public function handle(Request $request, Closure $next): Response|RedirectResponse|JsonResponse
{
$checks = [
empty(DB::connection()->getDatabaseName()) => 'setup.index',
empty(User::exists()) => 'setup.admin',
empty(DB::connection()->getDatabaseName()) => [
'redirect' => 'setup.index',
'patterns' => [
'setup.index',
'setup.test',
'setup.save',
],
],
empty(User::exists()) => [
'redirect' => 'setup.admin',
'patterns' => 'setup.admin*',
],
];
foreach ($checks as $check => $route) {
if ($check) {
return $request->routeIs($route) ? $next($request) : redirect()->route($route);
return $request->routeIs($route['patterns']) ? $next($request) : redirect()->route($route['redirect']);
}
}