src/Controller/Frontend/UserController.php line 24
<?phpnamespace App\Controller\Frontend;use App\Entity\User;use App\Form\Type\UserRegisterType;use App\Repository\UserRepository;use Symfony\Bridge\Twig\Mime\TemplatedEmail;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Mailer\MailerInterface;use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;use SymfonyCasts\Bundle\VerifyEmail\Exception\ExpiredSignatureException;use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;class UserController extends AbstractController{#[Route('/register', name: 'user.register', methods: ['GET', 'POST'])]public function register(Request $request,UserRepository $repo,UserPasswordHasherInterface $userPasswordHasher,VerifyEmailHelperInterface $verifyEmailHelper,MailerInterface $mailer): Response{$user = new User();$form = $this->createForm(UserRegisterType::class, $user);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$user = $form->getData();$existingUser = $repo->findOneBy(['email' => $user->getEmail()]);if ($existingUser) {$this->addFlash('danger', 'User with that email already exists!');return $this->redirectToRoute('user.register');}$user->setPassword($userPasswordHasher->hashPassword($user,$form->get('plain_password')->getData()));$repo->save($user, true);$signatureComponents = $verifyEmailHelper->generateSignature('user.register.confirm',$user->getId(),$user->getEmail(),['id' => $user->getId()] // add the user's id as an extra query param);$email = (new TemplatedEmail())->to($user->getEmail())->subject('Confirm your email address')->htmlTemplate('Frontend/Mail/RegisterConfirm.twig')->context(['signed_url' => $signatureComponents->getSignedUrl(),'expiration_date' => new \DateTime('+7 days'),]);$mailer->send($email);$this->addFlash('success', 'Thanks for your registration! You will receive a mail with a link to confirm your email address shortly.');return $this->redirectToRoute('video.index');}return $this->render('Frontend/User/Register.twig', ['form' => $form,]);}#[Route('/register-confirm', name: 'user.register.confirm', methods: ['GET'])]public function registerConfirm(Request $request,UserRepository $repo,VerifyEmailHelperInterface $verifyEmailHelper){$id = $request->get('id');if (!$id) {$this->addFlash('danger', 'Invalid params (1)!');return $this->redirectToRoute('user.register');}$user = $repo->find($id);if (!$user) {$this->addFlash('danger', 'Invalid params (2)!');return $this->redirectToRoute('user.register');}try {$verifyEmailHelper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getEmail());} catch (VerifyEmailExceptionInterface $e) {if ($e instanceof ExpiredSignatureException) {$this->addFlash('danger', 'Link has already expired!');} else {$this->addFlash('danger', 'Invalid params (1)!');}return $this->redirectToRoute('user.register');}$this->addFlash('success', 'Thanks, your registration is now complete!');return $this->redirectToRoute('user.login');}// TODO PasswordReset// TODO PasswordChange#[Route('/login', name: 'user.login', methods: ['GET', 'POST'])]public function login(Request $request, AuthenticationUtils $authenticationUtils){// get the login error if there is one$error = $authenticationUtils->getLastAuthenticationError();// last username entered by the user$lastUsername = $authenticationUtils->getLastUsername();return $this->render('Frontend/User/Login.twig', ['last_username' => $lastUsername,'error' => $error,]);}#[Route('/logout', name: 'user.logout', methods: ['GET'])]public function logout(){// controller can be blank: it will never be called!throw new \Exception('Don\'t forget to activate logout in security.yaml');}}