• Stars
    star
    342
  • Rank 123,348 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple, stylish Email Verification for Symfony

VerifyEmailBundle: Love Confirming Emails

Don't know if your users have a valid email address? The VerifyEmailBundle can help!

VerifyEmailBundle generates - and validates - a secure, signed URL that can be emailed to users to confirm their email address. It does this without needing any storage, so you can use your existing entities with minor modifications. This bundle provides:

  • A generator to create a signed URL that should be emailed to the user.
  • A signed URL validator.
  • Peace of mind knowing that this is done without leaking the user's email address into your server logs (avoiding PII problems).

Installation

Using Composer of course!

composer require symfonycasts/verify-email-bundle

Usage

We strongly suggest using Symfony MakerBundle's make:registration-form command to get a feel for how the bundle should be used. It's super simple! Answer a couple questions, and you'll have a fully functional secure registration system with email verification.

bin/console make:registration-form

Setting Things Up Manually

If you want to set things up manually, you can! But do so carefully: email verification is a sensitive, security process. We'll guide you through the important stuff. Using make:registration-form is still the easiest and simplest way.

The example below demonstrates the basic steps to generate a signed URL that is to be emailed to a user after they have registered. The URL is then validated once the user "clicks" the link in their email.

The example below utilizes Symfony's AbstractController available in the FrameworkBundle:

// RegistrationController.php

use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
// ...

class RegistrationController extends AbstractController
{
    private $verifyEmailHelper;
    private $mailer;
    
    public function __construct(VerifyEmailHelperInterface $helper, MailerInterface $mailer)
    {
        $this->verifyEmailHelper = $helper;
        $this->mailer = $mailer;
    }
    
    /**
     * @Route("/register", name="register-user")
     */
    public function register(): Response
    {
        $user = new User();
    
        // handle the user registration form and persist the new user...
    
        $signatureComponents = $this->verifyEmailHelper->generateSignature(
                'registration_confirmation_route',
                $user->getId(),
                $user->getEmail()
            );
        
        $email = new TemplatedEmail();
        $email->from('[email protected]');
        $email->to($user->getEmail());
        $email->htmlTemplate('registration/confirmation_email.html.twig');
        $email->context(['signedUrl' => $signatureComponents->getSignedUrl()]);
        
        $this->mailer->send($email);
    
        // generate and return a response for the browser
    }
}

Once the user has received their email and clicked on the link, the RegistrationController would then validate the signed URL in following method:

// RegistrationController.php

use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
// ...

class RegistrationController extends AbstractController
{
    // ...
    /**
     * @Route("/verify", name="registration_confirmation_route")
     */
    public function verifyUserEmail(Request $request): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
        $user = $this->getUser();

        // Do not get the User's Id or Email Address from the Request object
        try {
            $this->verifyEmailHelper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getEmail());
        } catch (VerifyEmailExceptionInterface $e) {
            $this->addFlash('verify_email_error', $e->getReason());

            return $this->redirectToRoute('app_register');
        }

        // Mark your user as verified. e.g. switch a User::verified property to true

        $this->addFlash('success', 'Your e-mail address has been verified.');

        return $this->redirectToRoute('app_home');
    }
}

Anonymous Validation

It is also possible to allow users to verify their email address without having to be authenticated. A use case for this would be if a user registers on their laptop, but clicks the verification link on their phone. Normally, the user would be required to log in before their email was verified.

We can overcome this by passing a user identifier as a query parameter in the signed url. The diff below demonstrate how this is done based off of the previous examples:

// RegistrationController.php

class RegistrationController extends AbstractController
{
    public function register(): Response
    {
        $user = new User();
    
        // handle the user registration form and persist the new user...
    
        $signatureComponents = $this->verifyEmailHelper->generateSignature(
                'registration_confirmation_route',
                $user->getId(),
-               $user->getEmail()
+               $user->getEmail(),
+               ['id' => $user->getId()] // add the user's id as an extra query param
            );
    }
}

Once the user has received their email and clicked on the link, the RegistrationController would then validate the signed URL in the following method:

// RegistrationController.php

+use App\Repository\UserRepository;

class RegistrationController extends AbstractController
{
-   public function verifyUserEmail(Request $request): Response
+   public function verifyUserEmail(Request $request, UserRepository $userRepository): Response
    {
-       $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
-       $user = $this->getUser();

+       $id = $request->get('id'); // retrieve the user id from the url
+
+       // Verify the user id exists and is not null
+       if (null === $id) {
+           return $this->redirectToRoute('app_home');
+       }
+
+       $user = $userRepository->find($id);
+
+       // Ensure the user exists in persistence
+       if (null === $user) {
+           return $this->redirectToRoute('app_home');
+       }

        try {
            $this->verifyEmailHelper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getEmail());
        } catch (VerifyEmailExceptionInterface $e) {
        // ...
    }
}

Configuration

You can change the default configuration parameters for the bundle by creating a config/packages/verify_email.yaml config file:

symfonycasts_verify_email:
    lifetime: 3600

lifetime

Optional - Defaults to 3600 seconds

This is the length of time a signed URL is valid for in seconds after it has been created.

Reserved Query Parameters

If you add any extra query parameters in the 5th argument of verifyEmailHelper::generateSignature(), such as we did for id above, take note that you cannot use the following query parameters, because they will be overwritten by this bundle:

  • token
  • expires
  • signature

Support

Feel free to open an issue for questions, problems, or suggestions with our bundle. Issues pertaining to Symfony's MakerBundle, specifically make:registration-form, should be addressed in the Symfony Maker repository.

Security Issues

For security related vulnerabilities, we ask that you send an email to ryan [at] symfonycasts.com instead of creating an issue.

This will give us the opportunity to address the issue without exposing the vulnerability before a fix can be published.

More Repositories

1

reset-password-bundle

Need a killer reset password feature for your Symfony? Us too!
PHP
402
star
2

messenger-monitor-bundle

Visual Monitoring & Retries for Symfony Messenger!
CSS
73
star
3

symfony5

Screencast code, script and sunshine behind the Symfony 5 tutorials!
Twig
56
star
4

api-platform

Screencast code, script and everlasting friendship behind the "API Platform" tutorial
PHP
29
star
5

vue

Screencast code, script and cupcakes behind the "The Delightful World of Vue.js" tutorial
PHP
16
star
6

symfony-ux

Screencast code, script and everlasting friendship behind the "Symfony UX: Stimulus" tutorial
PHP
13
star
7

dynamic-forms

Add dynamic/dependent fields to Symfony forms
PHP
10
star
8

symfony6

Screencast code, script and the macaroni and cheese behind the "Harmonious Development with Symfony 6" tutorial
Twig
8
star
9

messenger

Screencast code, script and unicorns behind the "Messenger! Queue work for Later" tutorial
PHP
7
star
10

EasyAdminBundle

Course code behind EasyAdminBundle v3
PHP
6
star
11

solid

Screencast code, script and puppies behind the "Write SOLID Code & Impress your Friends" tutorial
CSS
6
star
12

micro-mapper

A tiny, underwhelming data mapper for Symfony to map one object to another!
PHP
5
star
13

workshop_symfonycon18_encore

Nothing to see here! Just some code from the SymfonyCon 2018 Webpack Encore workshop
PHP
4
star
14

conferences

Metadata, transcripts and true grit behind the Symfony Conferences on SymfonyCasts
3
star
15

deep-symfony-dive

Screencast code, script and kittens behind the "Symfony 5 Deep Dive!" tutorial
JavaScript
3
star
16

dino-park

A place for us to track our dinosaurs πŸ¦•
3
star
17

vinyl-mixes

A "data" repository for our Symfony 6 Tutorials!
2
star
18

tailwind-bundle

Delightful Tailwind Support for Symfony + AssetMapper
PHP
2
star
19

workshop_symfonycon19_encore

Nothing to see here! Just some code from the SymfonyCon 2019 Webpack Encore workshop
PHP
2
star
20

sfcon2018

Screencast code, script and puppies behind the "SymfonyCon 2018 Lisbon Conference Videos" tutorial
2
star
21

design-patterns

Design Patterns tutorial code
PHP
2
star
22

symfony5-challenges

Challenges for "Charming Development in Symfony 5"
2
star
23

asset-mapper

Screencast code, script and freckles behind the "AssetMapper: Modern JS with Zero Build System" tutorial
PHP
1
star
24

testing-temi-integration

Nothing to see here folks!
1
star
25

turbo-challenges

Challenges for "Symfony UX: Turbo"
1
star
26

api-platform3

Screencast code, script and glitter behind the "API Platform 3" tutorial
PHP
1
star
27

symfony5-security-challenges

Challenges for "Symfony 5 Security: Authenticators"
1
star
28

.github

1
star
29

vue-challenges

Challenges for "The Delightful World of Vue.js"
1
star
30

blackfire

Screencast code, script and flower petals behind the "Blackfire.io: Revealing Performance Secrets with Profiling" tutorial
JavaScript
1
star
31

doctrine-queries

SQL, DQL, Query Builders and code for the "Query Like a Pro in Doctrine" series - updated version of https://github.com/knpuniversity/doctrine-queries
JavaScript
1
star
32

testing

Screencast code, script and true grit behind the "PHPUnit: Testing with a Bite!" tutorial
Twig
1
star