added OAuth2 Controller

This commit is contained in:
Charles 2025-04-17 16:45:35 +02:00
parent c1046ee6e0
commit 0d939ca9e8
1 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,41 @@
<?php
namespace App\Controller;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
class OAuth2Controller extends AbstractController
{
#[Route('/oauth/api/user', name: 'app_oauth_api_user')]
public function oauthApiUser(): JsonResponse
{
/** @var User $user */
$user = $this->getUser();
return new JsonResponse([
'message' => 'Authentification réussie !',
'email' => $user->getEmail(),
'name' => $user->getUsername(),
]);
}
#[Route('/oauth2/userinfo', name: 'userinfo', methods: ['GET'])]
public function userinfo(): JsonResponse
{
$user = $this->getUser();
if (!$user) {
return new JsonResponse(['error' => 'Unauthorized'], 401);
}
return new JsonResponse([
'sub' => $user->getId(),
'username' => $user->getUsername(),
'email' => $user->getEmail(),
'roles' => $user->getRoles(),
]);
}
}