MeController.php 820 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\UserModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. class MeController
  7. {
  8. private UserModel $userModel;
  9. public function __construct()
  10. {
  11. $this->userModel = new UserModel();
  12. }
  13. public function __invoke(ServerRequestInterface $request)
  14. {
  15. $userId = (int) ($request->getAttribute('user_id') ?? 0);
  16. if ($userId <= 0) {
  17. return ResponseLib::sendFail("Unauthorized: Missing authenticated user", [], "E_VALIDATE")->withStatus(401);
  18. }
  19. $profile = $this->userModel->getAuthenticatedProfile($userId);
  20. if (!$profile) {
  21. return ResponseLib::sendFail("User not found", [], "E_NOT_FOUND")->withStatus(404);
  22. }
  23. return ResponseLib::sendOk($profile);
  24. }
  25. }