/
Register.php
64 lines (55 loc) · 2.23 KB
/
Register.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace AppBundle\Action;
use AppBundle\Entity\Order;
use AppBundle\Entity\ApiUser;
use Doctrine\Common\Persistence\ManagerRegistry;
use FOS\UserBundle\Util\UserManipulator;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationSuccessResponse;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class Register
{
private $userManipulator;
private $jwtManager;
private $dispatcher;
public function __construct(UserManipulator $userManipulator, JWTManager $jwtManager, EventDispatcherInterface $dispatcher)
{
$this->userManipulator = $userManipulator;
$this->jwtManager = $jwtManager;
$this->dispatcher = $dispatcher;
}
/**
* @Route(
* path="/register",
* name="api_register"
* )
* @Method("POST")
*/
public function registerAction(Request $request)
{
$email = $request->request->get('_email');
$username = $request->request->get('_username');
$password = $request->request->get('_password');
try {
$user = $this->userManipulator->create($username, $password, $email, true, false);
$jwt = $this->jwtManager->create($user);
} catch (\Exception $e) {
// TODO Send JSON-LD response
throw new BadRequestHttpException($e);
}
// See Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler
$response = new JWTAuthenticationSuccessResponse($jwt);
$event = new AuthenticationSuccessEvent(['token' => $jwt], $user, $response);
$this->dispatcher->dispatch(Events::AUTHENTICATION_SUCCESS, $event);
$response->setData($event->getData());
return $response;
}
}