mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
Stuff i guess ?
This commit is contained in:
51
app/Controllers/Api/AuthController.php
Normal file
51
app/Controllers/Api/AuthController.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?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'
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user