Skip to content
Permalink
Browse files

Added code to store subscribers' emails in redis

  • Loading branch information...
davidmerfield committed Mar 12, 2019
1 parent 58aefef commit 4f4e3bb65cf2ec5ce39244c3c703dfbe0a146cce
Showing with 43 additions and 14 deletions.
  1. +42 −14 app/brochure/routes/news.js
  2. +1 −0 app/brochure/views/news/confirmed.html
@@ -6,6 +6,9 @@ var fs = require("fs-extra");
var marked = require("marked");
var helper = require("helper");
var parse = require("body-parser").urlencoded({ extended: false });
var uuid = require("uuid/v4");
var config = require("config");
var client = require("client");

news.get("/", loadDone, loadToDo, function(req, res) {
res.locals.title = "Blot / News";
@@ -17,32 +20,57 @@ news.get("/archive", loadDone, loadToDo, function(req, res) {
res.render("news/archive");
});

news.get("/sign-up", function(req ,res){
news.get("/sign-up", function(req, res) {
res.render("news/sign-up");
});

news.get("/confirm/:guid", function(req, res) {
client.get(
"newsletter:confirm:" + decodeURIComponent(req.params.guid),
function(err, email) {
if (err || !email) {
res.locals.error = err ? "EINVAL" : null;
}

client.sadd("newsletter:list", email, function(err) {
res.locals.email = email;
res.render("news/confirmed");
});
}
);
});

news.post("/sign-up", parse, function(req, res) {
var err;
var guid = encodeURIComponent(
uuid()
.split("-")
.join("")
);
var confirm = "https://" + config.host + "/news/confirm/" + guid;
var TTL = 60 * 60 * 24; // 1 day in seconds

if (!req.body || !req.body.email) {
err = new Error();
err.code = "ENOENT";
}

var confirm = "https://blot.development/news/confirm";

helper.email.SUBSCRIBE_CONFIRMATION(
null,
{ email: req.body.email.trim().toLowerCase(), confirm: confirm },
function(err) {
if (err) {
err = new Error();
err.code = "EINVAL";
client.setex("newsletter:confirm:" + guid, TTL, req.body.email, function(
err
) {
helper.email.SUBSCRIBE_CONFIRMATION(
null,
{ email: req.body.email.trim().toLowerCase(), confirm: confirm },
function(err) {
if (err) {
err = new Error();
err.code = "EINVAL";
}

res.redirect("/news/sign-up");
}

req.message("/news/sign-up");
}
);
);
});
});

function loadToDo(req, res, next) {
@@ -0,0 +1 @@
You have confirmed {{email}}

0 comments on commit 4f4e3bb

Please sign in to comment.
You can’t perform that action at this time.