To get a good reputation as a sender, one of the email best practices we encourage our users to implement is the double opt-in. It’s actually a very simple practice: after a user gives you his/her email, you store their information with an authentication token and a boolean variable.
The authentication token is just a randomly generated value. After the user signs up for your newsletter or service, you want to send a simple email asking them to click on a link. This link will have their token and take them to a URL that will make a request to the database, search for the same token, and set that user's boolean variable to "true." This means they have a good email address and you can start sending email to them.
So, how do you do it? Even though I described it in one paragraph, and it's as simple as it sounds, I decided to go ahead and make a Node.js application example. You can find the complete example here
, but let's build it step by step!
Make sure you have Node.js
, and mongodb
installed. To make this as easy as possible, we’re using express project generator
, so open your terminal and run:
npm install -g express-generator
We’re going with the default project template, so run:
That will create your project and give you some extra instructions. We'll follow them:
cd node-emailauth && npm install
Then we'll test the project to see what we got. Run:
Open your favorite browser and go to http://localhost:3000. You should see the following page:
Now, let’s create the routes we’ll need for our app. In the first screen, the user will be able to input their email address into a form and submit it. The second screen will load after the form is submitted, confirming that they were sent an email. The final screen will be accessed after the user clicks the link inside the email sent to them, showing that the address is confirmed.
We already have our first screen, so let’s adjust it to accept the user’s email. Go to the views folder and open the file index.jade. The default express project uses the jade templating engine
for HTML, so we’ll stick with that. Replace the contents with the following: