Итерация 1: каркас Slim 4 приложения
- Slim 4 + php-di контейнер с маршрутизацией - Docker Compose: app (PHP 8.3-FPM) + web (nginx) + db (MariaDB) - Phinx миграция: таблица users (uuid, username, password_hash) - Auth: login/logout с сессиями - Setup: создание первого пользователя - Dashboard с layout (Bootstrap 5.3) - CLI: bin/console, bin/run-scan-worker.php - Smoke test: scripts/check.sh - README.md, PLAN.md
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domovoy\Middleware;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Slim\Routing\RouteContext;
|
||||
|
||||
class AuthMiddleware
|
||||
{
|
||||
public function __invoke(
|
||||
ServerRequestInterface $request,
|
||||
\Psr\Http\Server\RequestHandlerInterface $handler
|
||||
): ResponseInterface {
|
||||
$path = $request->getUri()->getPath();
|
||||
|
||||
$publicPaths = ['/login', '/setup'];
|
||||
$isPublic = in_array($path, $publicPaths, true);
|
||||
|
||||
if (!isset($_SESSION['user_id']) && !$isPublic) {
|
||||
$response = new \Slim\Psr7\Response();
|
||||
return $response
|
||||
->withHeader('Location', '/login')
|
||||
->withStatus(302);
|
||||
}
|
||||
|
||||
// If logged in and trying to access login/setup, redirect to dashboard
|
||||
if (isset($_SESSION['user_id']) && $isPublic) {
|
||||
$response = new \Slim\Psr7\Response();
|
||||
return $response
|
||||
->withHeader('Location', '/dashboard')
|
||||
->withStatus(302);
|
||||
}
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user