mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Api;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use App\Controllers\Controller;
|
|
use App\Services\AuthService;
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
private AuthService $authService;
|
|
|
|
public function __construct(AuthService $authService)
|
|
{
|
|
$this->authService = $authService;
|
|
}
|
|
|
|
/**
|
|
* Check if user is authenticated (API endpoint)
|
|
*/
|
|
public function checkAuth(Request $request, Response $response, $args)
|
|
{
|
|
try {
|
|
if (!$this->authService->isLoggedIn()) {
|
|
return $this->jsonResponse($response->withStatus(401), [
|
|
'error' => '401 Forbidden'
|
|
]);
|
|
}
|
|
|
|
$user = $this->authService->getCurrentUser();
|
|
if (!$user) {
|
|
return $this->jsonResponse($response->withStatus(401), [
|
|
'error' => '401 Forbidden'
|
|
]);
|
|
}
|
|
|
|
return $this->jsonResponse($response, [
|
|
'id' => $user['id'],
|
|
'username' => $user['username'],
|
|
'email' => $user['email'],
|
|
'is_admin' => $this->authService->isAdmin()
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->jsonResponse($response->withStatus(500), [
|
|
'error' => 'Authentication check failed'
|
|
]);
|
|
}
|
|
}
|
|
}
|