Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Controller/MeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Controller;

use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class MeController extends AbstractController
{
public function __construct(private readonly Security $security)
{
}

public function __invoke(): JsonResponse
{
$user = $this->security->getUser();

if (!$user instanceof User) {
throw new UnauthorizedHttpException('Bearer', 'User is not authenticated.');
}

return new JsonResponse([
'id' => $user->getId(),
'email' => $user->getEmail(),
'firstname' => $user->getFirstname(),
'roles' => $user->getRoles()],
Response::HTTP_OK);
}
}
6 changes: 6 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Controller\MeController;
use App\Controller\RegistrationController;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
Expand All @@ -29,6 +30,11 @@
controller: RegistrationController::class,
name: 'registration',
),
new Get(
uriTemplate: '/me',
controller: MeController::class,
name: 'me'
),
new Patch(),
new Delete(),
new Get(),
Expand Down
1 change: 1 addition & 0 deletions tests/AuthDefaultTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AuthDefaultTestCase extends ApiTestCase
protected const string AUTH_REGISTER_ENDPOINT = '/auth/register';
protected const string AUTH_LOGIN_ENDPOINT = '/auth/login';
protected const string AUTH_REFRESH_TOKEN_ENDPOINT = '/auth/token/refresh';
protected const string ME_ENDPOINT = '/me';
protected const string PASSWORD = 'password';

protected array $defaultHeaders = ['Content-Type' => 'application/ld+json'];
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,17 @@ public function testDeleteUser(): void
$this->deleteRequest(self::USER_ENDPOINT.'/'.$userId, token: $adminToken);
$this->assertResponseStatusCodeSame(Response::HTTP_NO_CONTENT);
}

public function testMe(): void
{
$admin = $this->login();
$adminToken = $admin['token'];

$response = $this->getRequest(self::ME_ENDPOINT, token: $adminToken);
$data = $response->toArray();

$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertEquals('chlooe@skintrack.com', $data['email']);
$this->assertEquals('Chloé', $data['firstname']);
}
}