<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\VisitorEmailType;
use App\Repository\UserRepository;
use App\Service\Encryption;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
use Symfony\Component\Security\Http\LoginLink\LoginLinkNotification;
use \Symfony\Component\HttpFoundation\Cookie;
class SecurityController extends AbstractController
{
private Encryption $encryption;
public function __construct(Encryption $encryption)
{
$this->encryption = $encryption;
}
/**
* @Route("/connexion", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// if ($request->isMethod('POST')) {
// $email = $request->request->get('email');
// $user = $userRepository->findOneBy(['email' => $email]);
// $loginLinkDetails = $loginLinkHandler->createLoginLink($user);
// // create a notification based on the login link details
// $notification = new LoginLinkNotification(
// $loginLinkDetails,
// 'Welcome to MY WEBSITE!' // email subject
// );
// // create a recipient for this user
// // $recipient = new Recipient($user->getEmail());
// $recipient = new Recipient("doumbiayacouba055@gmail.com");
// // send the notification to the user
// $notifier->send($notification, $recipient);
// // render a "Login link is sent!" page
// return $this->render('security/login_link_sent.html.twig');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
// /**
// * @Route("/visiteur", name="app_front_visitor_registration_page")
// */
// public function requestLoginLink(NotifierInterface $notifier, LoginLinkHandlerInterface $loginLinkHandler, UserRepository $userRepository, Request $request)
// {
// //$entityManager = $doctrine->getManager();
// $user = new User();
// $form = $this->createForm(VisitorEmailType::class, $user);
// $form->handleRequest($request);
// if ($form->isSubmitted() && $form->isValid()) {
// // load the user in some way (e.g. using the form input)
// $email = $request->request->get('email');
// $user = $userRepository->findOneBy(['email' => $email]);
// // create a login link for $user this returns an instance
// // of LoginLinkDetails
// $loginLinkDetails = $loginLinkHandler->createLoginLink($user);
// //$loginLink = $loginLinkDetails->getUrl();
// // create a notification based on the login link details
// $notification = new LoginLinkNotification(
// $loginLinkDetails,
// 'Welcome to MY WEBSITE!' // email subject
// );
// // create a recipient for this user
// $recipient = new Recipient($user->getEmail());
// // send the notification to the user
// $notifier->send($notification, $recipient);
// // render a "Login link is sent!" page
// return $this->redirectToRoute('app_front_visitor_details_page');
// }
// return $this->render('front/pages/authentication/visitor_registration_details_page.html.twig', [
// 'form' => $form->createView(),
// ]);
// }
/**
* @Route("/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("/login_check", name="login_check")
*/
public function check()
{
throw new \LogicException('This code should never be reached');
}
/**
* @Route("/config/firebase", name="firebase_config")
*/
public function firebaseConfig(Request $request)
{
$encryptedData = [
'apiKey' => $_ENV['FIREBASE_API_KEY'],
'authDomain' => $_ENV['FIREBASE_AUTH_DOMAIN'],
'projectId' => $_ENV['FIREBASE_PROJECT_ID'],
'storageBucket' => $_ENV['FIREBASE_STORAGE_BUCKET'],
'messagingSenderId' => $_ENV['FIREBASE_MESSAGING_SENDER_ID'],
'appId' => $_ENV['FIREBASE_APP_ID'],
'measurementId' => $_ENV['FIREBASE_MEASUREMENT_ID'],
'vapidKey' => $_ENV['FIREBASE_VAPID_KEY'],
];
if ($request->query->get('sw') != "1") {
$dataToEncrypt = json_encode($encryptedData);
$encryptedData = $this->encryption->encrypt($dataToEncrypt);
$session = $request->getSession();
setcookie('firebase_config_iv', $encryptedData['iv'], time() + (86400 * 30), "/");
setcookie('firebase_config_data', $encryptedData['data'], time() + (86400 * 30), "/");
$session->set('firebase_config_iv', $encryptedData['iv']);
$session->set('firebase_config_data', $encryptedData['data']);
}
return new JsonResponse($encryptedData);
}
}