<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use App\Repository\UserRepository;
use App\Entity\User;
use Symfony\Component\Mailer\MailerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Mime\Email;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$errorMessage = $error ? $error->getMessage() : null;
// Personnaliser le message d'erreur
$errorMessage_verif = '';
return $this->render('security/login.html.twig',
['last_username' => $lastUsername, 'error' => $error,
'errorMessage' => $errorMessage,'errorMessage_verif' => $errorMessage_verif ]);
}
#[Route(path: '/login', name: 'app_login_admin')]
public function loginAdmin(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$errorMessage = $error ? $error->getMessage() : null;
// Personnaliser le message d'erreur
$errorMessage_verif = '';
return $this->render('security/login_admin.html.twig', ['last_username' => $lastUsername,
'error' => $error,
'errorMessage' => $errorMessage,'errorMessage_verif' => $errorMessage_verif ]);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
#[Route('/access-denied', name: 'access_denied')]
public function accessDenied(): Response
{
return $this->render('security/access_denied.html.twig');
}
#[Route('/resend-verification', name: 'app_resend_verification')]
public function resendVerificationEmail(
UserRepository $userRepository,
MailerInterface $mailer,
EntityManagerInterface $entityManager,
AuthenticationUtils $authenticationUtils
) {
$lastUsername = $authenticationUtils->getLastUsername();
//$user = $this->getUser();
$user = $userRepository->findOneBy(['email' => $lastUsername]);
/*
if (!$user instanceof UserInterface) {
return new JsonResponse(['message' => 'Utilisateur non connecté.'], 401);
}
*/
if ($user->isIsVerified()) {
$errorMessage_verif = 'Votre compte est déjà vérifié.';
$errorMessage='';
return $this->render('security/login.html.twig', ['last_username' => $lastUsername,'errorMessage_verif' => $errorMessage_verif ,'errorMessage' => $errorMessage ]);
//return new JsonResponse(['message' => 'Votre compte est déjà vérifié.'], 400);
}
//dd($lastUsername);
// Générer un nouveau token de vérification
$verificationToken = bin2hex(random_bytes(32));
$user->setConfirmationToken($verificationToken);
$entityManager->flush();
// Envoyer un nouvel email de confirmation
$email = (new Email())
->from('yobale@cassaconsulting.com')
->to($lastUsername)
->subject('Confirmez votre adresse email')
->html("Bonjour<br/>Pour vérifier votre Email,<br/><p>Cliquez sur ce lien :</p>
<a href='https://yobaale.com/verify/$verificationToken'>Confirmer mon email</a><br/>Cdt,<br/>Yôbaale");
$mailer->send($email);
$errorMessage_verif = 'Email de vérification envoyé.';
$errorMessage='';
return $this->render('security/login.html.twig', ['last_username' => $lastUsername,'errorMessage_verif' => $errorMessage_verif ,'errorMessage' => $errorMessage ]);
// return new JsonResponse(['message' => 'Email de vérification envoyé.']);
}
#[Route('/verify/{token}', name: 'app_verify_email')]
public function verifyEmail(string $token, UserRepository $userRepository, EntityManagerInterface $entityManager,AuthenticationUtils $authenticationUtils)
{
$user = $userRepository->findOneBy(['confirmationToken' => $token]);
$lastUsername = $authenticationUtils->getLastUsername();
if (!$user) {
// return new JsonResponse(['message' => 'Token invalide ou expiré.'], 400);
return $this->redirectToRoute('app_login');
}
$user->setConfirmationToken(null);
$user->setIsVerified(true);
$entityManager->flush();
// return new JsonResponse(['message' => 'Votre email a été vérifié avec succès.']);
$errorMessage_verif = 'Votre email a été vérifié avec succès.';
$errorMessage='';
return $this->render('security/login.html.twig', ['last_username' => $lastUsername,'errorMessage_verif' => $errorMessage_verif ,'errorMessage' => $errorMessage ]);
}
}