Skip to content
Permalink
Browse files

Updated email sign up flow

  • Loading branch information...
davidmerfield committed Mar 12, 2019
1 parent 7529c5e commit 4c0b9e867db0e4af4e273b5be68c387d0f617e58
@@ -15,61 +15,80 @@ news.get("/", loadDone, loadToDo, function(req, res) {
res.render("news");
});

news.get("/archive", loadDone, loadToDo, function(req, res) {
news.get("/archive", function(req, res) {
res.locals.title = "Blot / News";
res.render("news/archive");
});

news.get("/archive/:letter", function(req, res) {
res.locals.title = "Blot / News";
res.render("news/archive");
});

// The rest of these pages should not be cached
news.use(function(req, res, next) {
res.header("Cache-Control", "no-cache");
next();
});

news.get("/sign-up", function(req, res) {
if (req.session && req.session.newsletter_email) {
res.locals.email = req.session.newsletter_email;
delete req.session.newsletter_email;
}

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;
}
function confirmationKey(guid) {
return "newsletter:confirm:" + guid;
}

var listKey = "newsletter:list";
var TTL = 60 * 60 * 24; // 1 day in seconds

function confirmationLink(guid) {
return "https://" + config.host + "/news/confirm/" + guid;
}

news.get("/confirm/:guid", function(req, res, next) {
var guid = decodeURIComponent(req.params.guid);

client.get(confirmationKey(guid), function(err, email) {
if (err || !email) return next(err || new Error("No email"));

client.sadd("newsletter:list", email, function(err) {
res.locals.email = email;
res.render("news/confirmed");
});
}
);
client.sadd(listKey, email, function(err) {
if (err) return next(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
news.post("/sign-up", parse, function(req, res, next) {
var confirm, email, locals;
var guid = uuid();

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

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");
}
);
email = req.body.email.trim().toLowerCase();
guid = guid.split("-").join("");
guid = encodeURIComponent(guid);
confirm = confirmationLink(guid);
locals = { email: email, confirm: confirm };

client.setex(confirmationKey(guid), TTL, email, function(err) {
if (err) return next(err);

helper.email.SUBSCRIBE_CONFIRMATION(null, locals, function(err) {
if (err) return next(err);

req.session.newsletter_email = email;
res.redirect("/news/sign-up");
});
});
});

@@ -1 +1 @@
You have confirmed {{email}}
<p>Thank you for confirming <b>{{email}}</b>'s subscription to Blot's newsletter.</p>
@@ -175,14 +175,14 @@
}
</style>
<h1>News and updates</h1>
<p>The lists below give you an idea of what I'm working on. I send out an email each season with a summary of these changes. View the <a href="/news/archive">newsletter archive</a>. I also announce news on <a href="https://www.twitter.com/blot__">Twitter</a>. Please do <a href="/contact">contact me</a> if you have any questions.</p>
<p>The lists below give you an idea of what I'm working on. I send out an email each season with a summary of these changes. View the <a href="/news/archive">newsletter archive</a>. Please don't hesitate to <a href="/contact">contact me</a> if you have any questions.</p>

<p>
<form action="/news/sign-up" method="POST" style="max-width:33em;">
<form action="/news/sign-up" method="POST" style="max-width:35em;">
<label for="email">Your email</label>
<div style="display:flex">
<input type="text" id="email" name="email">
<input type="submit" value="Subscribe">
<input type="text" required id="email" name="email">
<input type="submit" value="Subscribe to the newsletter">
</div>
</form>
</p>
@@ -1,6 +1,3 @@
{{^error}}
<p>You have been signed up.</p>
{{/error}}
{{#error}}
<p>{{error}}</p>
{{/error}}
{{#email}}
<p>A confirmation email has been sent to <b>{{.}}</b>. Please click the link inside to confirm your subscription to Blot's newsletter.</p>
{{/email}}

0 comments on commit 4c0b9e8

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