<?php
namespace App\Controller;
use App\Form\LoginFormType;
use App\Repository\UserRepository;
use App\Security\EmailVerifier;
use App\Service\PasswordResetService;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
/**
* @Route("/auth", name="auth_")
*/
class AuthController extends AbstractController
{
private EmailVerifier $emailVerifier;
private PasswordResetService $passwordResetService;
public function __construct(EmailVerifier $emailVerifier, PasswordResetService $passwordResetService)
{
$this->emailVerifier = $emailVerifier;
$this->passwordResetService = $passwordResetService;
}
/**
* Link to this controller to start the "connect" process
*
* @Route("/irms", name="irms_start")
*/
public function connectAction(ClientRegistry $clientRegistry): RedirectResponse
{
return $clientRegistry
->getClient('irms')
->redirect();
}
/**
* @Route("/irms/callback", name="irms_callback")
*/
public function connectCheckAction(Request $request, ClientRegistry $clientRegistry)
{
// deliberately empty as the callback is handled by IrmsAuthenticator
}
/**
* @Route("/login", name="login")
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('main');
}
$form = $this->createForm(LoginFormType::class);
$form->handleRequest($request);
$error = $authenticationUtils->getLastAuthenticationError();
if (!$error) {
$this->redirectToRoute('main');
} else {
$this->addFlash('error', 'Error logging in, try again or contact your instructor for assistance.');
}
return $this->render('auth/login.html.twig', [
'loginForm' => $form->createView()
]);
}
/**
* @Route("/logout", name="logout", methods={"GET"})
* @throws Exception
*/
public function logout(): void
{
throw new Exception('Error logging out');
}
/**
* @Route("/verify/email", name="verify_email")
*/
public function verifyUserEmail(Request $request, UserRepository $userRepository): Response
{
$id = $request->get('id');
if ($id === null) {
$this->addFlash('error', 'Error verifying email, contact your instructor for assistance.');
return $this->redirectToRoute('auth_login');
}
$user = $userRepository->find($id);
if ($user === null) {
$this->addFlash('error', 'Error verifying email, contact your instructor for assistance.');
return $this->redirectToRoute('auth_login');
}
// validate email confirmation link, sets User::isVerified=true and persists
try {
$this->emailVerifier->handleEmailConfirmation($request, $user);
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('error', $exception->getReason());
return $this->redirectToRoute('auth_login');
}
$this->passwordResetService->sendResetEmail($user->getEmail());
$this->addFlash('success', 'Your email address has been verified. Please check your email for a link to set your password.');
return $this->redirectToRoute('auth_login');
}
}