feat: add /me endpoint

This commit is contained in:
Maël Gangloff
2024-07-18 12:36:01 +02:00
parent 9d88599d86
commit 2efc3da018
4 changed files with 54 additions and 7 deletions

View File

@@ -2,19 +2,25 @@ api_platform:
title: Domain Watchdog API
version: 1.0.0
formats:
jsonld: ['application/ld+json']
json: ['application/json']
jsonld: [ 'application/ld+json' ]
json: [ 'application/json' ]
docs_formats:
jsonld: ['application/ld+json']
jsonopenapi: ['application/vnd.openapi+json']
html: ['text/html']
jsonld: [ 'application/ld+json' ]
jsonopenapi: [ 'application/vnd.openapi+json' ]
html: [ 'text/html' ]
defaults:
stateless: true
cache_headers:
vary: ['Content-Type', 'Authorization', 'Origin']
vary: [ 'Content-Type', 'Authorization', 'Origin' ]
extra_properties:
standard_put: true
rfc_7807_compliant_errors: true
keep_legacy_inflector: false
use_symfony_listeners: true
show_webby: false
swagger:
api_keys:
JWT:
name: Authorization
type: header

View File

@@ -17,7 +17,7 @@ security:
pattern: ^/api/login
stateless: true
json_login:
check_path: /api/login
check_path: api_login
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class MeController extends AbstractController
{
public function __construct(
private readonly Security $security
)
{
}
public function __invoke(): UserInterface
{
return $this->security->getUser();
}
}

View File

@@ -2,6 +2,10 @@
namespace App\Entity;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Controller\MeController;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
@@ -9,10 +13,22 @@ use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Attribute\Groups;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
#[ApiResource(
operations: [
new Get(
uriTemplate: '/me',
controller: MeController::class,
paginationEnabled: false,
normalizationContext: ["groups" => "user:list"],
read: false
)
]
)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
@@ -21,12 +37,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
private ?int $id = null;
#[ORM\Column(length: 180)]
#[Groups(['user:list'])]
private ?string $email = null;
/**
* @var array The user roles
*/
#[ORM\Column]
#[Groups(['user:list'])]
private array $roles = [];
/**