add: webseite mit jwt
This commit is contained in:
12
webseite-react-php-jwt/slim-php/public/.htaccess
Normal file
12
webseite-react-php-jwt/slim-php/public/.htaccess
Normal file
@@ -0,0 +1,12 @@
|
||||
# MAMP/Apache FastCGI verwirft Authorization sonst, bevor PHP es sieht.
|
||||
CGIPassAuth On
|
||||
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
|
||||
|
||||
DirectoryIndex index.php
|
||||
RewriteEngine On
|
||||
RewriteCond %{HTTP:Authorization} .
|
||||
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
|
||||
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
RewriteRule ^ index.php [QSA,L]
|
||||
75
webseite-react-php-jwt/slim-php/public/index.php
Normal file
75
webseite-react-php-jwt/slim-php/public/index.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Auth\JwtService;
|
||||
use App\Config;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
use App\Middleware\CorsMiddleware;
|
||||
use App\Repository\ProductRepository;
|
||||
use App\Repository\UserRepository;
|
||||
use DI\Container;
|
||||
use Slim\Factory\AppFactory;
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__));
|
||||
$dotenv->safeLoad();
|
||||
|
||||
$container = new Container();
|
||||
$container->set(PDO::class, static fn () => Config::pdo());
|
||||
$container->set(
|
||||
ProductRepository::class,
|
||||
static fn ($c) => new ProductRepository($c->get(PDO::class))
|
||||
);
|
||||
$container->set(
|
||||
UserRepository::class,
|
||||
static fn ($c) => new UserRepository($c->get(PDO::class))
|
||||
);
|
||||
$container->set(
|
||||
JwtService::class,
|
||||
static fn () => new JwtService(
|
||||
$_ENV['JWT_SECRET'] ?? '',
|
||||
(int) ($_ENV['JWT_TTL'] ?? 86400)
|
||||
)
|
||||
);
|
||||
$container->set(
|
||||
AuthMiddleware::class,
|
||||
static fn ($c) => new AuthMiddleware(
|
||||
$c->get(JwtService::class),
|
||||
$c->get(UserRepository::class)
|
||||
)
|
||||
);
|
||||
|
||||
AppFactory::setContainer($container);
|
||||
$app = AppFactory::create();
|
||||
|
||||
$basePath = Config::slimBasePath();
|
||||
if ($basePath !== '') {
|
||||
$app->setBasePath($basePath);
|
||||
}
|
||||
|
||||
$app->addBodyParsingMiddleware();
|
||||
$app->addRoutingMiddleware();
|
||||
$app->addErrorMiddleware(true, true, true);
|
||||
$app->add(new CorsMiddleware());
|
||||
|
||||
(require __DIR__ . '/../src/Routes/productRoutes.php')($app);
|
||||
(require __DIR__ . '/../src/Routes/userRoutes.php')($app);
|
||||
|
||||
if (empty($_SERVER['HTTP_AUTHORIZATION'])) {
|
||||
$redirected = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ?? '';
|
||||
|
||||
if ($redirected !== '') {
|
||||
$_SERVER['HTTP_AUTHORIZATION'] = $redirected;
|
||||
} elseif (function_exists('getallheaders')) {
|
||||
foreach (getallheaders() as $name => $value) {
|
||||
if (strcasecmp((string) $name, 'Authorization') === 0 && is_string($value) && $value !== '') {
|
||||
$_SERVER['HTTP_AUTHORIZATION'] = $value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$app->run();
|
||||
11
webseite-react-php-jwt/slim-php/public/router.php
Normal file
11
webseite-react-php-jwt/slim-php/public/router.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/');
|
||||
|
||||
if ($uri !== '/' && file_exists(__DIR__ . $uri)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
require __DIR__ . '/index.php';
|
||||
Reference in New Issue
Block a user