src/Controller/AuthController.php line 65
<?php
namespace App\Controller;
use App\Controller\Bootstrap\DefaultLayoutController;
use App\Entity\User;
use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\RedirectResponse;
class AuthController extends DefaultLayoutController
{
public function signin(AuthenticationUtils $authenticationUtils): Response
{
if($redirectResponse = $this->redirectIfLogged()) return $redirectResponse;
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
$this->theme->addJavascriptFile('js/custom/authentication/sign-in/general.js');
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('pages/auth/signin.html.twig', [
'controller_name' => 'AuthController',
'last_username' => $lastUsername,
'error' => $error,
'_target_path' => $this->generateUrl('dashboard', []),
]);
}
public function signup(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
if($redirectResponse = $this->redirectIfLogged()) return $redirectResponse;
$this->theme->addJavascriptFile('js/custom/authentication/sign-up/general.js');
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('dashboard');
}
return $this->render('pages/auth/signup.html.twig', [
'registrationForm' => $form->createView(),
]);
}
public function reset_password(): Response
{
if($redirectResponse = $this->redirectIfLogged()) return $redirectResponse;
$this->theme->addJavascriptFile('js/custom/authentication/reset-password/reset-password.js');
return $this->render('pages/auth/reset-password.html.twig');
}
public function new_password(): Response
{
if($redirectResponse = $this->redirectIfLogged()) return $redirectResponse;
$this->theme->addJavascriptFile('js/custom/authentication/reset-password/new-password.js');
return $this->render('pages/auth/new-password.html.twig');
}
protected function redirectIfLogged()
{
if($this->getUser()){
return new RedirectResponse($this->generateUrl('dashboard', []));
}
return null;
}
}