finished user prfile and passwords
This commit is contained in:
@@ -2,46 +2,117 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use ApiPlatform\Core\Api\IriConverterInterface;
|
||||
use App\Entity\User;
|
||||
use App\Form\LoginFormType;
|
||||
use App\Form\RegistrationFormType;
|
||||
use App\Repository\UserRepository;
|
||||
use App\Security\EmailVerifier;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Config\Definition\Exception\Exception;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Mime\Address;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class SecurityController extends AbstractController
|
||||
{
|
||||
#[Route(path: '/login', name: 'app_login')] // *** method post
|
||||
public function login(AuthenticationUtils $authenticationUtils, IriConverterInterface $iriConverter): Response
|
||||
{
|
||||
|
||||
/** @var User $user */
|
||||
$user = $this->getUser() ?? null;
|
||||
|
||||
return new Response(content: null, status: 204, headers: [
|
||||
'Location' => $iriConverter->getIriFromItem(item: $user)
|
||||
]);
|
||||
|
||||
public function __construct(private readonly EmailVerifier $emailVerifier)
|
||||
{
|
||||
// empty body
|
||||
}
|
||||
|
||||
/*
|
||||
return $this->render(view: 'security/login.html.twig', parameters: [
|
||||
'error' => $authenticationUtils->getLastAuthenticationError(),
|
||||
'last_username' => $authenticationUtils->getLastUsername(),
|
||||
|
||||
#[Route(path: '/login', name: 'app_login')]
|
||||
public function index(AuthenticationUtils $authenticationUtils): Response
|
||||
{
|
||||
// get the login error if there is one
|
||||
if ($error = $authenticationUtils->getLastAuthenticationError()) {
|
||||
$this->addFlash(type: 'error', message: $error->getMessageKey());
|
||||
}
|
||||
|
||||
// last username entered by the user
|
||||
$lastUsername = $authenticationUtils->getLastUsername();
|
||||
|
||||
return $this->render(view: '@default/security/login.html.twig', parameters: [
|
||||
'last_username' => $lastUsername,
|
||||
'error' => '',
|
||||
]);
|
||||
*
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
#[Route(path: '/logout', name: 'app_logout')]
|
||||
public function logout(): never
|
||||
|
||||
#[Route(path: '/register', name: 'app_register')]
|
||||
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
throw new Exception(message: 'Logout should never be reached.');
|
||||
$form = $this->createForm(type: RegistrationFormType::class);
|
||||
$form->handleRequest(request: $request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$user = $form->getData();
|
||||
// hash the plain password
|
||||
$user->setPassword(
|
||||
password: $userPasswordHasher->hashPassword(
|
||||
user: $user,
|
||||
plainPassword: $form->get(name: 'password')->getData()
|
||||
)
|
||||
);
|
||||
|
||||
if ($form->get(name: 'agreeTerms')->getData()) {
|
||||
$user->agreeTerms();
|
||||
} // no else, we already confirmed in the form itself
|
||||
$entityManager->persist(entity: $user);
|
||||
$entityManager->flush();
|
||||
|
||||
// generate a signed url and email it to the user
|
||||
$this->emailVerifier->sendEmailConfirmation(verifyEmailRouteName: 'app_verify_email', user: $user,
|
||||
email: (new TemplatedEmail())
|
||||
->from(new Address(address: 'info@24unix.net', name: '24unix.net'))
|
||||
->to($user->getEmail())
|
||||
->subject(subject: 'Please Confirm your Email')
|
||||
->htmlTemplate(template: '@default/security/mail/registration.html.twig')
|
||||
->context(context: [
|
||||
'username' => $user->getUsername()
|
||||
])
|
||||
);
|
||||
|
||||
return $this->render(view: '@default/security/registration_finished.html.twig');
|
||||
}
|
||||
|
||||
return $this->render(view: '@default/security/register.html.twig', parameters: [
|
||||
'registrationForm' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(path: '/verify/email', name: 'app_verify_email')]
|
||||
public function verifyUserEmail(Request $request, TranslatorInterface $translator, UserRepository $userRepository): Response
|
||||
{
|
||||
$id = $request->get(key: 'id');
|
||||
|
||||
if ($id === null) {
|
||||
return $this->redirectToRoute(route: 'app_register');
|
||||
}
|
||||
|
||||
$user = $userRepository->find($id);
|
||||
|
||||
if ($user === null) {
|
||||
return $this->redirectToRoute(route: 'app_register');
|
||||
}
|
||||
|
||||
// validate email confirmation link, sets User::isVerified=true and persists
|
||||
try {
|
||||
$this->emailVerifier->handleEmailConfirmation(request: $request, user: $user);
|
||||
} catch (VerifyEmailExceptionInterface $exception) {
|
||||
$this->addFlash(type: 'verify_email_error', message: $translator->trans(id: $exception->getReason(), parameters: [], domain: 'VerifyEmailBundle'));
|
||||
|
||||
return $this->redirectToRoute(route: 'app_main');
|
||||
}
|
||||
|
||||
$this->addFlash(type: 'success', message: 'Your email address has been verified.');
|
||||
|
||||
return $this->redirectToRoute(route: 'app_main');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user