Enhance API functionality and improve JWT authentication

- Added JWT authentication support in AuthService and JwtService.
- Implemented token generation and refresh mechanisms.
- Updated ApiAuthMiddleware to handle authentication for protected routes.
- Created ApiController and BaseApiController for standardized API responses.
- Developed MediaController for managing media items with pagination and search capabilities.
- Introduced DocsController for serving API documentation via Swagger UI.
- Added routes for API documentation and media management.
- Improved error handling and response formatting across API endpoints.
- Updated composer.json to include necessary JWT and Swagger UI dependencies.
This commit is contained in:
Lars Behrends
2025-12-31 10:08:49 +01:00
parent 1b053148f0
commit b728b0c72d
18 changed files with 858 additions and 27 deletions

42
routes/api-docs.php Normal file
View File

@@ -0,0 +1,42 @@
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteCollectorProxy;
use App\Controllers\Api\DocsController;
// Documentation routes
$app->group('/docs', function (RouteCollectorProxy $group) {
$docsController = $this->get(DocsController::class);
// Documentation UI
$group->get('/api', [$docsController, 'showDocs']);
// OpenAPI JSON specification
$group->get('/api-docs.json', [$docsController, 'getOpenApiSpec']);
// Serve Swagger UI assets
$group->get('/swagger-ui/{file:.+}', function (Request $request, Response $response, array $args) {
$file = $args['file'];
$swaggerUiPath = __DIR__ . '/../../vendor/swagger-api/swagger-ui/dist';
$filePath = $swaggerUiPath . '/' . $file;
if (!file_exists($filePath)) {
return $response->withStatus(404, 'File not found');
}
$extension = pathinfo($file, PATHINFO_EXTENSION);
$contentTypes = [
'css' => 'text/css',
'js' => 'application/javascript',
'png' => 'image/png',
'json' => 'application/json',
'html' => 'text/html',
];
$contentType = $contentTypes[$extension] ?? 'text/plain';
$response->getBody()->write(file_get_contents($filePath));
return $response->withHeader('Content-Type', $contentType);
});
});

89
routes/api2.php Normal file
View File

@@ -0,0 +1,89 @@
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteCollectorProxy;
use App\Middleware\ApiAuthMiddleware;
use App\Controllers\Api\MediaController;
use App\Controllers\Api\AuthController;
use App\Services\AuthService;
use App\Services\JwtService;
use App\Controllers\Api\DocsController;
// Get container
$container = $app->getContainer();
// API routes group
$app->group('/api', function (RouteCollectorProxy $group) use ($container) {
$docsController = $this->get(DocsController::class);
// Public endpoints
$group->get('/status', function (Request $request, Response $response) {
$response->getBody()->write(json_encode([
'status' => 'ok',
'timestamp' => time(),
'version' => '1.0.0'
]));
return $response->withHeader('Content-Type', 'application/json');
});
$group->get('/doku', [$docsController, 'getOpenApiSpec']);
$group->get('/docu', [$docsController, 'showDocs']);
// Auth routes
$group->group('/auth', function (RouteCollectorProxy $group) use ($container) {
$authController = $container->get(AuthController::class);
$group->post('/login', [$authController, 'login']);
$group->post('/register', [$authController, 'register']);
$group->post('/refresh', [$authController, 'refreshToken']);
$group->get('/me', [$authController, 'getCurrentUser'])
->add(new ApiAuthMiddleware($container->get(AuthService::class)));
});
// Protected routes (require authentication)
$group->group('', function (RouteCollectorProxy $group) use ($container) {
$mediaController = $container->get(MediaController::class);
// Games
$group->get('/games', [$mediaController, 'listGames']);
$group->get('/games/{id:[0-9]+}', [$mediaController, 'getGame']);
// Movies
$group->get('/movies', [$mediaController, 'listMovies']);
$group->get('/movies/{id:[0-9]+}', [$mediaController, 'getMovie']);
// TV Shows
$group->get('/tvshows', [$mediaController, 'listTvShows']);
$group->get('/tvshows/{id:[0-9]+}', [$mediaController, 'getTvShow']);
// Search
$group->get('/search', [$mediaController, 'search']);
})->add(new ApiAuthMiddleware($container->get(AuthService::class)));
// Admin routes (require admin role)
$group->group('/admin', function (RouteCollectorProxy $group) use ($container) {
// Add admin-specific routes here
$group->get('/users', function (Request $request, Response $response) {
// Admin-only user listing
$response->getBody()->write(json_encode(['message' => 'Admin access granted']));
return $response->withHeader('Content-Type', 'application/json');
});
})->add(new ApiAuthMiddleware($container->get(AuthService::class)));
});
// Add CORS middleware
$app->add(function (Request $request, $handler) {
$response = $handler->handle($request);
return $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
});
// Handle preflight requests
$app->options('/{routes:.+}', function (Request $request, Response $response, $args) {
return $response;
});

View File

@@ -74,15 +74,15 @@ $app->group('', function (RouteCollectorProxy $group) {
$group->get('', AdminController::class . ':movies')->setName('admin.movies.index');
$group->map(['GET', 'POST'], '/create', AdminController::class . ':editMovie')->setName('admin.movies.create');
$group->map(['GET', 'POST'], '/{id}/edit', AdminController::class . ':editMovie')->setName('admin.movies.edit');
$group->delete('/{id}', AdminController::class . ':deleteMovie')->setName('admin.movies.delete');
$group->map(['POST', 'DELETE'], '/{id}', AdminController::class . ':deleteMovie')->setName('admin.movies.delete');
});
$adminGroup->group('/games', function (RouteCollectorProxy $group) {
$group->get('', AdminController::class . ':games')->setName('admin.games.index');
$group->map(['GET', 'POST'], '/create', AdminController::class . ':editGame')->setName('admin.games.create');
$group->map(['GET', 'POST'], '/{id}/edit', AdminController::class . ':editGame')->setName('admin.games.edit');
$group->delete('/{id}', AdminController::class . ':deleteGame')->setName('admin.games.delete');
$group->map(['POST', 'DELETE'], '/{id}', AdminController::class . ':deleteGame')->setName('admin.games.delete');
// SteamGridDB API routes
$group->group('/sgdb', function (RouteCollectorProxy $sgdb) {
$sgdb->get('/search', 'App\Controllers\GameController:searchSteamGridDb')->setName('admin.games.sgdb.search');
@@ -95,7 +95,7 @@ $app->group('', function (RouteCollectorProxy $group) {
$group->get('', AdminController::class . ':shows')->setName('admin.shows.index');
$group->map(['GET', 'POST'], '/create', AdminController::class . ':editShow')->setName('admin.shows.create');
$group->map(['GET', 'POST'], '/{id}/edit', AdminController::class . ':editShow')->setName('admin.shows.edit');
$group->delete('/{id}', AdminController::class . ':deleteShow')->setName('admin.shows.delete');
$group->map(['POST', 'DELETE'], '/{id}', AdminController::class . ':deleteShow')->setName('admin.shows.delete');
});
$adminGroup->group('/adult', function (RouteCollectorProxy $group) {
@@ -128,8 +128,8 @@ $app->group('', function (RouteCollectorProxy $group) {
$sourcesGroup->post('', 'App\Controllers\MediaSourceController:store')->setName('admin.sources.store');
$sourcesGroup->get('/{id:\d+}/edit', 'App\Controllers\MediaSourceController:edit')->setName('admin.sources.edit');
$sourcesGroup->post('/{id:\d+}', 'App\Controllers\MediaSourceController:update')->setName('admin.sources.update');
$sourcesGroup->delete('/{id:\d+}', 'App\Controllers\MediaSourceController:destroy')->setName('admin.sources.destroy');
$sourcesGroup->post('/{id:\d+}/delete', 'App\Controllers\MediaSourceController:destroy')->setName('admin.sources.destroy');
// Source sync operations
$sourcesGroup->post('/{id:\d+}/sync', 'App\Controllers\MediaSourceController:startSync')->setName('admin.sources.sync');
$sourcesGroup->get('/sync/status/{log_id}', 'App\Controllers\MediaSourceController:syncStatus')->setName('admin.sources.sync.status');