Skip to content

Commit 76db145

Browse files
committed
fix: feedback from PR thephpleague#229
1 parent afa3cda commit 76db145

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

docs/device-code-grant.md

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Password grant handling
1+
# Device grant handling
22

33
The device code grant type is designed for devices without a browser or with limited input capabilities. In this flow, the user authenticates on another device—like a smartphone or computer—and receives a code to enter on the original device.
44

@@ -23,47 +23,40 @@ namespace App\Controller;
2323

2424
use League\Bundle\OAuth2ServerBundle\Repository\DeviceCodeRepository;
2525
use League\OAuth2\Server\Exception\OAuthServerException;
26-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
27-
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
2826
use Symfony\Component\Form\Extension\Core\Type\TextType;
2927
use Symfony\Component\HttpFoundation\Request;
3028
use Symfony\Component\HttpFoundation\Response;
3129
use Symfony\Component\Routing\Attribute\Route;
3230

33-
class DeviceCodeController extends AbstractController
34-
{
35-
36-
public function __construct(
37-
private readonly DeviceCodeRepository $deviceCodeRepository
38-
) {
39-
}
31+
public function __construct(
32+
private readonly DeviceCodeRepository $deviceCodeRepository
33+
) {
34+
}
4035

41-
#[Route(path: '/verify-device', name: 'app_verify_device', methods: ['GET', 'POST'])]
42-
public function verifyDevice(
43-
Request $request
44-
): Response {
45-
$form = $this->createFormBuilder()
46-
->add('userCode', TextType::class, [
47-
'required' => true,
48-
])
49-
->getForm()
50-
->handleRequest($request);
51-
52-
if ($form->isSubmitted() && $form->isValid()) {
53-
try {
54-
$this->deviceCodeRepository->approveDeviceCode($form->get('userCode')->getData(), $this->getUser()->getId());
55-
// Device code approved, show success message to user
56-
} catch (OAuthServerException $e) {
57-
// Handle exception (invalid code or missing user ID)
58-
}
36+
#[Route(path: '/verify-device', name: 'app_verify_device', methods: ['GET', 'POST'])]
37+
public function verifyDevice(
38+
Request $request
39+
): Response {
40+
$form = $this->createFormBuilder()
41+
->add('userCode', TextType::class, [
42+
'required' => true,
43+
])
44+
->getForm()
45+
->handleRequest($request);
46+
47+
if ($form->isSubmitted() && $form->isValid()) {
48+
try {
49+
$this->deviceCodeRepository->approveDeviceCode($form->get('userCode')->getData(), $this->getUser()->getId());
50+
// Device code approved, show success message to user
51+
} catch (OAuthServerException $e) {
52+
// Handle exception (invalid code or missing user ID)
5953
}
60-
61-
return $this->render(
62-
'verify_device.html.twig',
63-
['form' => $form]
64-
);
6554
}
6655

56+
return $this->render(
57+
'verify_device.html.twig',
58+
['form' => $form]
59+
);
6760
}
6861
```
6962

src/Manager/Doctrine/DeviceCodeManager.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public function findByUserCode(string $code): ?DeviceCodeInterface
3030
{
3131
/** @var ?DeviceCodeInterface */
3232
return $this->entityManager->createQueryBuilder()
33-
->select('dc')
34-
->from(DeviceCode::class, 'dc')
35-
->where('dc.userCode = :code')
36-
->setParameter('code', $code)
37-
->getQuery()
38-
->getOneOrNullResult();
33+
->select('dc')
34+
->from(DeviceCode::class, 'dc')
35+
->where('dc.userCode = :code')
36+
->setParameter('code', $code)
37+
->getQuery()
38+
->getOneOrNullResult();
3939
}
4040

4141
public function save(DeviceCodeInterface $deviceCode, bool $persist = true): void

0 commit comments

Comments
 (0)