Auth, Login, Home, Venta->Listados->Precios

This commit is contained in:
Juan Pablo Vial
2023-07-24 20:55:26 -04:00
parent d9d5a15376
commit 1a7b10ce3c
130 changed files with 4302 additions and 0 deletions

View File

@ -0,0 +1,48 @@
<?php
namespace Incoviba\Middleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Incoviba\Service;
class Authentication
{
public function __construct(protected ResponseFactoryInterface $responseFactory, protected Service\Login $service, protected string $login_url) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->service->isIn() or $this->isValid($request)) {
return $handler->handle($request);
}
$response = $this->responseFactory->createResponse(301, 'Not logged in');
return $response->withHeader('Location', $this->login_url)
->withHeader('X-Redirected-URI', (string) $request->getUri());
}
protected function isValid(ServerRequestInterface $request): bool
{
$uri = $request->getUri();
$current_path = $uri->getPath();
$current_url = implode('', [
"{$uri->getScheme()}://",
$uri->getHost() . ($uri->getPort() !== null ? ":{$uri->getPort()}" : ''),
$uri->getPath()
]);
$valid_paths = [
'/',
];
if (in_array($current_path, $valid_paths, true)) {
return true;
}
$valid_uris = [
$this->login_url,
];
if (in_array($current_url, $valid_uris, true)) {
return true;
}
return false;
}
}