-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistrationController.php
More file actions
77 lines (64 loc) · 2.55 KB
/
Copy pathRegistrationController.php
File metadata and controls
77 lines (64 loc) · 2.55 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Security\Registration;
use App\Security\RegistrationException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
final class RegistrationController extends AbstractController
{
public function __construct(private readonly Registration $registration)
{
}
#[Route(path: '/register', name: 'app_register', methods: ['GET', 'POST'])]
public function register(Request $request): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('app_frontpage');
}
$submitted = [
'email' => '',
'name' => '',
];
$error = null;
$status = Response::HTTP_OK;
if ('POST' === $request->getMethod()) {
$submitted['email'] = (string) $request->request->get('email', '');
$submitted['name'] = (string) $request->request->get('name', '');
$plainPassword = (string) $request->request->get('password', '');
$plainPasswordConfirm = (string) $request->request->get('password_confirm', '');
if (!$this->isCsrfTokenValid('register', (string) $request->request->get('_token'))) {
return $this->render('registration/register.html.twig', [
'submitted' => $submitted,
'error' => 'register.error.invalid_token',
], new Response('', Response::HTTP_FORBIDDEN));
}
try {
$this->registration->register(
$submitted['email'],
$submitted['name'],
$plainPassword,
$plainPasswordConfirm,
);
return $this->redirectToRoute('app_register_pending');
} catch (RegistrationException $e) {
$error = $e->getMessage();
$status = Response::HTTP_UNPROCESSABLE_ENTITY;
}
}
return $this->render('registration/register.html.twig', [
'submitted' => $submitted,
'error' => $error,
], new Response('', $status));
}
#[Route(path: '/register/pending', name: 'app_register_pending', methods: ['GET'])]
public function pending(): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('app_frontpage');
}
return $this->render('registration/pending.html.twig');
}
}