src/Controller/LoginController.php line 95
<?php
namespace App\Controller;
use App\Form\ChooseFormType;
use App\Repository\UserRepository;
use App\Services\Codes\SendCode;
use App\Services\Mails\Mails;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController
{
public function __construct(private UserRepository $userRepository,private SendCode $sendCode)
{
}
#[Route(path: [
"fr" => "/login_site",
"en" => "/en/login_site",
], name: 'app_login_site')]
public function index(Request $request, EntityManagerInterface $entityManager, Mails $mails, UserPasswordHasherInterface $userPasswordHasher)
{
$form = $this->createForm(ChooseFormType::class);
$handleRequest = $form->handleRequest($request);
$error = $request->query->get('error');
if (is_null($error)) {
$error = false;
}
$before = $request->query->get('before') ?? $request->headers->get('referer');
$chemin = parse_url($before, PHP_URL_PATH);
if($chemin == "/login_site")$before = null;
if ($handleRequest->isSubmitted() && $handleRequest->isValid()) {
$identifier = $form->get('identifier')->getData();
$prefix = $form->get('prefix')->getData();
$code = (string)random_int(100000, 999999);
// check email
$userEmail = $this->userRepository->findOneBy(['email' => $identifier]);
if ($userEmail) {
if (!$userEmail->isIsActive()) {
$this->addFlash('error', 'Votre compte est désactivé Pour toute information contacter contact@roomlers.com');
return $this->redirectToRoute('app_login_site', ['error' => $error, 'before' => $before]);
} else {
$userEmail->setPassword($userPasswordHasher->hashPassword($userEmail, $code));
$entityManager->persist($userEmail);
$entityManager->flush();
$email = $mails->createEmail('mails/security/AuthentificationCode.html.twig', ["code" => $code])->to($identifier)->subject('Votre code de connexion Roomlers');
$mails->send($email);
return $this->redirectToRoute('app_code', ['identifier' => $identifier, 'before' => $before]);
}
}
// check phone
$userPhone = $this->userRepository->findOneBy(['phone' => $identifier]);
if ($userPhone) {
if (!$userPhone->isIsActive()) {
$this->addFlash('danger', 'Votre compte est désactivé Pour toute information contacter contact@roomlers.com');
return $this->redirectToRoute('app_login_site', ['error' => $error, 'before' => $before]);
} else {
$this->sendCode->send($identifier, $prefix, $code);
$userPhone->setPassword($userPasswordHasher->hashPassword($userPhone, $code));
$entityManager->persist($userPhone);
$entityManager->flush();
return $this->redirectToRoute('app_code', ['identifier' => $userPhone->getEmail(), 'before' => $before]);
}
}
$error = true;
$this->addFlash('danger', 'Identifiant inconnu');
return $this->redirectToRoute('app_login_site', ['error' => $error, 'before' => $before]);
}
return $this->render('login/index.html.twig', [
'controller_name' => 'LoginController',
'form' => $form->createView(),
'error' => $error,
'before' => $before,
]);
}
#[Route(path: [
"fr" => "/code",
"en" => "/en/code",
], name: 'app_code')]
public function code(Request $request, AuthenticationUtils $authenticationUtils)
{
$session = $request->getSession();
$before = $session->get('before');
$session->remove('before');
if ($before) {
return new RedirectResponse($before);
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
if ($request->query->get('error')) {
$error = $request->query->get('error');
}
if ($request->query->get('identifier')) {
$lastUsername = $request->query->get('identifier');
} else {
if (!$error) {
return $this->redirectToRoute('app_login_site', ['before' => $before]);
} else {
return $this->redirectToRoute('app_code', ['identifier' => $lastUsername, 'error' => $error, 'before' => $before]);
}
}
return $this->render('login/code.html.twig', [
'controller_name' => 'LoginController',
'last_username' => $lastUsername,
'error' => $error,
'before' => $before,
]);
}
}