This commit is contained in:
Juan Pablo Vial
2023-07-24 20:41:38 -04:00
parent 6ab24c8961
commit be33305cf1
612 changed files with 11436 additions and 107 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace App\Middleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Views\Blade as View;
use App\Service;
class Auth
{
public function __construct(protected Service\Auth $service, protected ResponseFactoryInterface $responseFactory, protected View $view) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->skipRoutes($request) or $this->service->isIn()) {
return $handler->handle($request);
}
return $this->redirect('guest');
}
protected function skipRoutes(ServerRequestInterface $request): bool
{
$uri = $request->getUri();
$route = $uri->getPath();
$routes = [
'/guest',
'/auth/login'
];
return in_array($route, $routes, true);
}
protected function redirect(string $route): ResponseInterface
{
$response = $this->responseFactory->createResponse(301, 'Not authorized');
return $this->view->render($response, $route);
}
}