Codementor Events

How to implement Email Verification feature in your NodeJS app using Express, SendGrid, Sequelize ORM(MySQL).

Published Aug 17, 2018
How to implement Email Verification feature in your NodeJS app using Express, SendGrid, Sequelize ORM(MySQL).

This tutorial is going to give you a high-level guide on how to get users of your NodeJS/Express web application get their email verified for possible foreseeable future occurrences where users can manage their accounts or recover their passwords and also make sure they are not robots with fake emails.

GENERAL OVERVIEW

For this tutorial, MySQL is used as the database and SendGrid as our library for sending emails.
Basically to achieve email verification is actually a simple straightforward process of doing the following:
User signs up into application.
A user cannot sign in yet into application until their email is verified.
A user receives an email with a verification link that contains a token.
User clicks on verification link to get redirected to the application where the token is used to verify them.

So this is the workflow we will be following throughout this tutorial:
Generating Models (ie. User and VerificationToken) with Sequelize
Defining controllers to handle operations (ie. signing up, send email verification using SendGrid).
Add a verification route.

GENERATING MODELS

  • Creating User model
    The caveat here with our user model is that we will add a boolean column, isVerified, to tell apart users that have been verified from those that have not. Our isVerified column will have a default value of false.
    Using sequelize-cli, we generate the User model
sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string, password:string,isVerified:boolean

So in your models' directory, you find a user.js file but another caveat to be mindful of is there is a one-to-one relationship between User and VerificationToken model so in our associate function we will add a snippet to indicate that relationship.

alt

This will also generate a migration file for us

alt

Now we generate another model for handling our verification tokens and here we introduce a foreign key constraint, userId to reference the User table

sequelize model:generate --name VericationToken --attributes userId:integer,token:string

alt

In our migration file, we will indicate that column userId in the VerificationToken table to references User table.

It is also important to note we are going to add a query to create an event, expireToken on line 32, that will delete tokens that are a day old.

alt

DEFINING CONTROLLERS TO HANDLE OPERATIONS

Before we proceed to create our controllers, we need to create a helper function to send our emails using SendGrid. First off, register on SendGrid and acquire your API key and store as an env variable process.env.SendGridApiKey.

alt

Now we create our sign up controller, a package called crypto-random-string will be added to enable us to generate random strings for our token.

alt

We now create the verification controller,

alt

CREATING ROUTES

Now we add the following routes,

alt

CONCLUSION

That is all for our high-level guide on the general concept of how to implement email verification using SendGrid and Sequelize ORM. Also, keep in mind based on your web application specifications your implementation may differ.
Feel free to leave feedback in the comments or reach out to me on Twitter.

CHEERS!!

Discover and read more posts from Audax Anchirinah
get started