<?php
namespace App\Controller;
use App\Entity\UserVerificationTokens;
use App\Entity\Users;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class AuthController extends AbstractController
{
#[Route('/login', name: 'login')]
public function login(Request $request, AuthenticationUtils $authenticationUtils)
{
if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
return $this->redirectToRoute('users');
}
$errors = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'error' => $errors,
'last_username' => $lastUsername,
]);
}
/**
* @Route("/login_success", name="login_success")
*/
public function postLoginRedirectAction()
{
if ($this->isGranted('ROLE_PARTNER')) {
return $this->redirectToRoute('app_partners_vouchers');
}
if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
return $this->redirectToRoute('users');
}
}
/**
* @Route("/logout", name="logout")
*/
public function logout()
{
}
#[Route('/email_verification/{token}', name: 'email_verification')]
public function emailVerificationPage($token, Request $request, EntityManagerInterface $em) {
$lang = $request->get('lang', 'ro');
$user = $em
->getRepository(Users::class)
->getByVerificationToken($token);
if (null === $user) {
return $this->render(
"account_creation/verificationFailure". ucfirst($lang) . ".html.twig",
['lang' => $lang]
);
}
// verify user
$user->setEmailVerified(true);
$user->setUpdatedAt(new \DateTime());
// invalidate token
$em->getRepository(UserVerificationTokens::class)->deleteByToken($token);
$em->flush();
return $this->render(
"account_creation/verificationSuccess". ucfirst($lang) . ".html.twig",
['lang' => $user->getLang()]
);
}
// /**
// * @Route("/fix", name="fix")
// */
// public function fixAction(UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $em)
// {
// $user = new Administrators();
// $user->setEmail('andrei');
// $user->setUsername('andrei');
// $user->setFirstname('Andrei');
// $user->setLastname('Admin');
// $user->setCreated(new \DateTime());
// $user->setActive(true);
//
// $encoded = $passwordHasher->hashPassword($user, '2019@Andrei');
// $user->setPassword($encoded);
//
// $em->persist($user);
// $em->flush();
// }
}