<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\Category;
use App\Entity\PaymentT;
use App\Service\EmailService;
use App\Security\EmailVerifier;
use App\Form\RegistrationFormType;
use App\Security\AppAuthenticator;
use Symfony\Component\Mime\Address;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Translation\Translator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Translation\LocaleSwitcher;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
// use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
class RegistrationController extends BaseCommonController
{
private EmailVerifier $emailVerifier;
private $emailService;
public function __construct(EmailVerifier $emailVerifier, EmailService $emailService)
{
$this->emailVerifier = $emailVerifier;
$this->emailService = $emailService;
}
#[Route('/send-test-email', name: 'send_test_email')]
public function sendTestEmail(): Response
{
$this->emailService->sendEmail('georgeskayal2014@gmail.com', 'Test Email', 'This is a test email.');
return new Response('Email sent successfully');
}
private function generateSignedUrl(User $user): string
{
// Your logic to generate a signed URL
// Example: Generate a unique token, add it to the URL, and sign it
// (Remember to handle expiration and security considerations)
// ...
return 'https://example.com/signed-url'; // Replace with your actual URL
}
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, AppAuthenticator $authenticator,
EntityManagerInterface $entityManager,ManagerRegistry $doctrine,
EmailService $mail,LocaleSwitcher $localeSwitcher,TranslatorInterface $translator,MailerInterface $mailer ): Response
{
// $currentLocale = $localeSwitcher->getLocale();
// $locale = $request->get('_locale');
$locale = $request->getSession()->get('_locale');
if($locale !=null){
$localeSwitcher->setLocale($locale);
$translator->setLocale($locale);
$request->getSession()->set('_locale',$locale);
}
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$pt = $entityManager->getRepository(PaymentT::class)->find(1);
$user->setPaymentT($pt);
$entityManager->persist($user);
$entityManager->flush();
// $signedUrl = $this->emailVerifier->generateSignedUrl('app_verify_email', $user);
// test email
// test email
// Send the email with the signed URL
// $email = (new TemplatedEmail())
// ->from('VerifyMyAccount@julico.io')
// ->to(new Address($user->getEmail()))
// ->subject('Confirm your email address')
// ->htmlTemplate('registration/confirmation_email.html.twig')
// ->context([
// 'signedUrl' => $signedUrl,
// ]);
try{
// // generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
(new TemplatedEmail())
// Verify20241
->from(new Address('no-reply@julico.io', 'Admin App'))
->to($user->getEmail())
->subject('Please Confirm your Email')
->htmlTemplate('registration/confirmation_email.html.twig')
// ->context([
// 'resetToken' => $resetToken,
// 'username' => $user->getPrenom(),
// ]);
// $mailer->send($email);
// ->context([
// 'signedUrl' => $signedUrl,
// ])
);
// var_dump("after send" . "send email");die;
// $urlemail = 'reset_password/email'. $locale .'.html.twig';
// // dd($urlemail);
// $email = (new TemplatedEmail())
// ->from(new Address('no-reply@julico.io', 'Julico Reset'))
// ->to($user->getEmail())
// ->subject('Reset your Julico password')
// ->htmlTemplate($urlemail)
// ->context([
// 'resetToken' => $resetToken,
// 'username' => $user->getPrenom(),
// ]);
// $mailer->send($email);
// // do anything else you need here, like send an email
}
catch (Exception $e) {
// Handle the exception
// You can log the error or show a flash message
// For example:
// $this->addFlash('error', 'An error occurred while saving the entity.');
var_dump("exception" + $exception);die;
}
// $mail->sendEmail('VerifyMyAccount@julico.io',
// $user->getEmail(),
// 'Activation de votre compte',
// 'confirmation_email',
// compact('user')
// );
return $userAuthenticator->authenticateUser(
$user,
$authenticator,
$request
);
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView()
]);
}
#[Route('/verify/email', name: 'app_verify_email')]
public function verifyUserEmail(Request $request, TranslatorInterface $translator): Response
{
// var_dump("dans app verify ");die;
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
// validate email confirmation link, sets User::isVerified=true and persists
try {
$this->emailVerifier->handleEmailConfirmation($request, $this->getUser());
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
return $this->redirectToRoute('app_register');
}
// @TODO Change the redirect on success and handle or remove the flash message in your templates
$this->addFlash('success', 'Your email address has been verified.');
return $this->redirectToRoute('app_register');
}
}