Skip to content

Commit

Permalink
Security: send registration confirmation email.
Browse files Browse the repository at this point in the history
  • Loading branch information
zerai committed Feb 17, 2024
1 parent a872a10 commit 4219758
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
1 change: 1 addition & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<PossiblyNullArgument>
<code><![CDATA[$user->getEmail()]]></code>
<code><![CDATA[$user->getEmail()]]></code>
<code><![CDATA[$user->getEmail()]]></code>
</PossiblyNullArgument>
</file>
<file src="src/Controller/Security/LogoutController.php">
Expand Down
29 changes: 21 additions & 8 deletions src/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Uid\UuidV4;
Expand All @@ -31,7 +33,7 @@
class RegistrationController extends AbstractController
{
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, VerifyEmailHelperInterface $verifyEmailHelper): Response
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager, VerifyEmailHelperInterface $verifyEmailHelper, MailerInterface $mailer): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
Expand Down Expand Up @@ -60,13 +62,10 @@ public function register(Request $request, UserPasswordHasherInterface $userPass
]
);

// TEMP: REMOVE LATER
$this->addFlash('success', 'Confirm your email at: ' . $signatureComponent->getSignedUrl());
$this->sendConfirmationEmail($mailer, $signatureComponent->getSignedUrl(), $user->getEmail());
$this->addFlash('info', 'Please check your mail and confirm your email address!');

// do anything else you need here, like send an email
//TODO SEND VERIFY MAIL

return $this->redirectToRoute('app_index');
return $this->redirectToRoute('app_login');
}

return $this->render('registration/register.html.twig', [
Expand Down Expand Up @@ -105,8 +104,22 @@ public function verifyUserEmail(Request $request, VerifyEmailHelperInterface $ve
}

#[Route('/verify/resend', name: 'app_verify_resend_email')]
public function resendVerifyEmail(): Response
public function resendVerifyEmail(Request $request): Response
{
//TODO: add form with email field
return $this->render('registration/resend_verify_email.html.twig');
}

private function sendConfirmationEmail(MailerInterface $mailer, string $signedUrl, string $userEmail): void
{
$email = (new Email())
->from('[email protected]')
->to($userEmail)
->priority(Email::PRIORITY_HIGH)
->subject('Auth oe-modules.com: please confirm your registration')
->text('Confirm your email at: ' . $signedUrl)
->html('<p> Confirm your email at: ' . $signedUrl . '</p>');

$mailer->send($email);
}
}
18 changes: 18 additions & 0 deletions templates/common/_flash_messages.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% if app.request.hasPreviousSession %}
<div class="container col-sm-auto col-6 mt-1 justify-content-center">
<div class="row">
{% for type, messages in app.flashes %}
{% for message in messages %}
{# Bootstrap alert, see https://getbootstrap.com/docs/5.3/components/alerts/ #}
<div class="alert alert-dismissible alert-{{ type }} fade show" role="alert">
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">

</button>

{{ message|trans }}
</div>
{% endfor %}
{% endfor %}
</div>
</div>
{% endif %}
13 changes: 1 addition & 12 deletions templates/layout-base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,7 @@
{% include 'common/navigation.html.twig' %}
{% endblock %}

{% for flash in app.flashes('success') %}
<div class="alert alert-success">
{{ flash }}
</div>
{% endfor %}
{% for flash in app.flashes('error') %}
<div class="alert alert-danger">
{{ flash }}
</div>
{% endfor %}


{% include 'common/_flash_messages.html.twig' %}

{% block main %}
<main class="flex-shrink-0">
Expand Down
3 changes: 3 additions & 0 deletions templates/security/login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

{% block body %}
<main class="form-signin w-100 m-auto">

{% include 'common/_flash_messages.html.twig' %}

<form action="{{ path('app_login') }}" method="post">
{# <img class="mb-4" src="/docs/5.3/assets/brand/bootstrap-logo.svg" alt="" width="72" height="57">#}
<h1 class="h3 mb-3 fw-normal">Please sign in</h1>
Expand Down

0 comments on commit 4219758

Please sign in to comment.