diff --git a/src/Controller/MeController.php b/src/Controller/MeController.php new file mode 100644 index 0000000..f5ed4b7 --- /dev/null +++ b/src/Controller/MeController.php @@ -0,0 +1,33 @@ +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); + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php index c115c91..e3ab8b0 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -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; @@ -29,6 +30,11 @@ controller: RegistrationController::class, name: 'registration', ), + new Get( + uriTemplate: '/me', + controller: MeController::class, + name: 'me' + ), new Patch(), new Delete(), new Get(), diff --git a/tests/AuthDefaultTestCase.php b/tests/AuthDefaultTestCase.php index e563495..0a5b4e3 100644 --- a/tests/AuthDefaultTestCase.php +++ b/tests/AuthDefaultTestCase.php @@ -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']; diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 0b0320e..23bd319 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -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']); + } }