Skip to content

Commit

Permalink
Update add new email flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesleyjanenorton committed Apr 3, 2019
1 parent 68c33b5 commit 461351f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 59 deletions.
23 changes: 15 additions & 8 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const DB = require("../db/DB");
const EmailUtils = require("../email-utils");
const { FluentError } = require("../locale-utils");
const FXA = require("../lib/fxa");
const HBSHelpers = require("../template-helpers/hbs-helpers");
const HBSHelpers = require("../template-helpers/");
const HIBP = require("../hibp");
const sha1 = require("../sha1-utils");

Expand All @@ -33,7 +33,7 @@ async function add(req, res) {
req.session.user, email, fxNewsletter, signupLanguage
);

/* TODO: restore when email templates are working

await EmailUtils.sendEmail(
email,
req.fluentFormat("user-add-email-verify-subject"),
Expand All @@ -45,17 +45,22 @@ async function add(req, res) {
whichView: "email_partials/email_verify",
}
);
*/


res.send({
title: req.fluentFormat("user-add-title"),
});
}

const getDashboard = (req, res) => {
res.render("dashboard", {
title: req.fluentFormat("user-dash"),
});
};


async function _verify(req) {
const verifiedEmailHash = await DB.verifyEmailHash(req.query.token);

let unsafeBreachesForEmail = [];
unsafeBreachesForEmail = await HIBP.getBreachesForEmail(
sha1(verifiedEmailHash.email),
Expand All @@ -72,7 +77,7 @@ async function _verify(req) {
{
email: verifiedEmailHash.email,
supportedLocales: req.supportedLocales,
date: HBSHelpers.prettyDate(req.supportedLocales, new Date()),
date: HBSHelpers.e_prettyDate(req.supportedLocales, new Date()),
unsafeBreachesForEmail: unsafeBreachesForEmail,
scanAnotherEmailHref: EmailUtils.getScanAnotherEmailUrl(utmID),
unsubscribeUrl: EmailUtils.getUnsubscribeUrl(verifiedEmailHash, utmID),
Expand All @@ -87,11 +92,12 @@ async function verify(req, res) {
if (!req.query.token) {
throw new FluentError("user-verify-token-error");
}
const existingSubscriber = await DB.getSubscriberByToken(req.query.token);
if (!existingSubscriber) {
const existingEmail = await DB.getEmailByToken(req.query.token);

if (!existingEmail) {
throw new FluentError("error-not-subscribed");
}
if (!existingSubscriber.verified) {
if (!existingEmail.verified) {
await _verify(req);
}

Expand Down Expand Up @@ -188,6 +194,7 @@ function logout(req, res) {

module.exports = {
getPreferences,
getDashboard,
add,
verify,
getUnsubscribe,
Expand Down
31 changes: 26 additions & 5 deletions db/DB.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ const DB = {
return res[0];
},

async getEmailByToken(token) {
const res = await knex("email_addresses")
.where("verification_token", "=", token);

return res[0];
},

async getSubscriberByTokenAndHash(token, emailSha1) {
const res = await knex.table("subscribers")
.first()
Expand Down Expand Up @@ -56,12 +63,12 @@ const DB = {
},

async verifyEmailHash(token) {
const unverifiedSubscriber = await this.getSubscriberByToken(token);
if (!unverifiedSubscriber) {
throw new FluentError("error-not-subscribed");
const unverifiedEmail = await this.getEmailByToken(token);
if (!unverifiedEmail) {
throw new FluentError("Error message for this verification email timed out or something went wrong.");
}
const verifiedSubscriber = await this._verifySubscriber(unverifiedSubscriber);
return verifiedSubscriber[0];
const verifiedEmail = await this._verifyNewEmail(unverifiedEmail);
return verifiedEmail[0];
},

// TODO: refactor into an upsert? https://jaketrent.com/post/upsert-knexjs/
Expand Down Expand Up @@ -160,6 +167,20 @@ const DB = {
return verifiedSubscriber;
},

// Verifies new emails added by existing users
async _verifyNewEmail(emailHash) {
await HIBP.subscribeHash(emailHash.sha1);

const verifiedEmail = await knex("email_addresses")
.where("email", "=", emailHash.email)
.update({
verified: true,
})
.returning("*");

return verifiedEmail;
},

/**
* Update fxa_refresh_token and fxa_profile_json for subscriber
*
Expand Down
5 changes: 3 additions & 2 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ const express = require("express");
const bodyParser = require("body-parser");

const { asyncMiddleware } = require("../middleware");
const { getPreferences, add, verify, getUnsubscribe, postUnsubscribe, getUnsubSurvey, postUnsubSurvey, logout } = require("../controllers/user");
const { getDashboard,getPreferences, add, verify, getUnsubscribe, postUnsubscribe, getUnsubSurvey, postUnsubSurvey, logout } = require("../controllers/user");

const router = express.Router();
const jsonParser = bodyParser.json();
const urlEncodedParser = bodyParser.urlencoded({ extended: false });


router.get("/dashboard", getDashboard);
router.get("/preferences", getPreferences);
router.get("/logout", logout);
router.post("/email", urlEncodedParser, asyncMiddleware(add));
router.get("/email/verify", jsonParser, asyncMiddleware(verify));
router.get("/verify", jsonParser, asyncMiddleware(verify));
router.use("/email/unsubscribe", urlEncodedParser);
router.get("/email/unsubscribe", asyncMiddleware(getUnsubscribe));
router.post("/email/unsubscribe", asyncMiddleware(postUnsubscribe));
Expand Down
8 changes: 8 additions & 0 deletions views/dashboard.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<main class="container dash">
<div class="row">
<div class="col-6">
<p class="welcome-back">Welcome back <span class="bold">{{ req.session.user.fxa_profile_json.email }}</span></p>
<h2 class="skinny-headline">Breach Summary</h2>
</div>
</div>
</main>
35 changes: 3 additions & 32 deletions views/partials/subpages/confirm.hbs
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
<section id="confirmation" class="confirmation whole">
<ul class="sharing-options">
<li class="show-email-options">
<a target="_blank" {{> analytics/share_by_email eventLabel=emailLinks.default-email.client}} rel="noopener noreferrer" href="{{ emailLinks.default-email.href }}">
<span class="sharing-option email">
{{{getString "share-email"}}}
</span>
</a>
<div class="email-options">
{{#each emailLinks}}
<a {{> analytics/share_by_email eventLabel=this.client }} target="_blank" rel="noopener noreferrer" href="{{ this.href }}" >
<span class="email-option {{ this.class }}">{{ this.client }}</span>
</a>
{{/each}}
</div>
</li>
<li>
<a class="facebook" href="https://www.facebook.com/sharer/sharer.php?u={{ SERVER_URL }}" target="_blank" rel="noopener noreferrer" {{> analytics/social_event eventLabel="Facebook"}}>
<span class="sharing-option facebook">
{{{getString "share"}}}
</span>
</a>
</li>
<li>
<a class="twitter" href="https://twitter.com/intent/tweet?url={{ SERVER_URL }}" target="_blank" rel="noopener noreferrer" {{> analytics/social_event eventLabel="Twitter"}}>
<span class="sharing-option twitter">
{{{getString "tweet"}}}
</span>
</a>
</li>
</ul>
</section>
<h1>EMAIL IS CONFIRMED!</h1>

<a href="/user/dashboard">Back to Dashboard</a>
14 changes: 2 additions & 12 deletions views/subpage.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@

<section id="subpage" class="subpage container" data-analytics-id="{{ analyticsID }}">
<section class="row-full-width flx-col">
{{#ifCompare hash "&&" FXA_ENABLED}}
<h2 class="section-headline whole" >{{getString "fxa-unsub-headline"}}</h2>
<p class="sub-head whole">
{{getString "fxa-unsub-blurb"}}
<a class="manage-fxa" href="{{getFxaUrl}}" target="_blank" rel="noopener">
{{getString "manage-fxa"}}
</a>
</p>
{{else}}
<h2 class="subpage-headline" >{{{ headline }}}</h2>
<p class="subpage-subhead">{{{ subhead }}}</p>
{{/ifCompare}}
<h2 class="subpage-headline" >{{{ headline }}}</h2>
<p class="subpage-subhead">{{{ subhead }}}</p>
{{#if whichPartial}}
{{> (lookup . 'whichPartial') }}
{{/if}}
Expand Down

0 comments on commit 461351f

Please sign in to comment.