From 2b490e00d7874e9b15c5a09649daafac9c47bdf0 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Wed, 25 Sep 2019 14:18:50 +0200 Subject: [PATCH] wrap email templates in standard layout to minimize duplicate code --- .../src/middleware/email/emailMiddleware.js | 13 +- .../src/middleware/email/templateBuilder.js | 77 ++ .../email/templates/emailVerification.html | 189 +++++ .../src/middleware/email/templates/index.js | 11 + .../middleware/email/templates/layout.html | 256 +++++++ .../email/templates/resetPassword.html | 613 +++++----------- .../middleware/email/templates/signup.html | 675 ++++++------------ .../email/templates/templateBuilder.js | 48 -- .../email/templates/wrongAccount.html | 613 +++++----------- 9 files changed, 1101 insertions(+), 1394 deletions(-) create mode 100644 backend/src/middleware/email/templateBuilder.js create mode 100644 backend/src/middleware/email/templates/emailVerification.html create mode 100644 backend/src/middleware/email/templates/index.js create mode 100644 backend/src/middleware/email/templates/layout.html delete mode 100644 backend/src/middleware/email/templates/templateBuilder.js diff --git a/backend/src/middleware/email/emailMiddleware.js b/backend/src/middleware/email/emailMiddleware.js index bea1bf9b3..4096fa83a 100644 --- a/backend/src/middleware/email/emailMiddleware.js +++ b/backend/src/middleware/email/emailMiddleware.js @@ -5,7 +5,8 @@ import { signupTemplate, resetPasswordTemplate, wrongAccountTemplate, -} from './templates/templateBuilder' + emailVerificationTemplate, +} from './templateBuilder' const hasEmailConfig = CONFIG.SMTP_HOST && CONFIG.SMTP_PORT const hasAuthData = CONFIG.SMTP_USERNAME && CONFIG.SMTP_PASSWORD @@ -57,8 +58,18 @@ const sendPasswordResetMail = async (resolve, root, args, context, resolveInfo) return true } +const sendEmailVerificationMail = async (resolve, root, args, context, resolveInfo) => { + const response = await resolve(root, args, context, resolveInfo) + // TODO: return name in response + const { email, nonce, name } = response + await sendMail(emailVerificationTemplate({ email, nonce, name })) + delete response.nonce + return response +} + export default { Mutation: { + AddEmailAddress: sendEmailVerificationMail, requestPasswordReset: sendPasswordResetMail, Signup: sendSignupMail, SignupByInvitation: sendSignupMail, diff --git a/backend/src/middleware/email/templateBuilder.js b/backend/src/middleware/email/templateBuilder.js new file mode 100644 index 000000000..4b7bcc7cd --- /dev/null +++ b/backend/src/middleware/email/templateBuilder.js @@ -0,0 +1,77 @@ +import mustache from 'mustache' +import CONFIG from '../../config' + +import * as templates from './templates' + +const from = '"Human Connection" ' +const supportUrl = 'https://human-connection.org/en/contact' + +export const signupTemplate = ({ email, nonce }) => { + const subject = 'Willkommen, Bienvenue, Welcome to Human Connection!' + const actionUrl = new URL('/registration/create-user-account', CONFIG.CLIENT_URI) + actionUrl.searchParams.set('nonce', nonce) + actionUrl.searchParams.set('email', email) + + return { + from, + to: email, + subject, + html: mustache.render( + templates.layout, + { actionUrl, supportUrl, subject }, + { content: templates.signup }, + ), + } +} + +export const emailVerificationTemplate = ({ email, nonce, name }) => { + const subject = 'Neue E-Mail Adresse | New E-Mail Address' + const actionUrl = new URL('/settings/my-email-address/verify', CONFIG.CLIENT_URI) + actionUrl.searchParams.set('nonce', nonce) + actionUrl.searchParams.set('email', email) + + return { + from, + to: email, + subject, + html: mustache.render( + templates.layout, + { actionUrl, name, nonce, supportUrl, subject }, + { content: templates.emailVerification }, + ), + } +} + +export const resetPasswordTemplate = ({ email, nonce, name }) => { + const subject = 'Neues Passwort | Reset Password' + const actionUrl = new URL('/password-reset/change-password', CONFIG.CLIENT_URI) + actionUrl.searchParams.set('nonce', nonce) + actionUrl.searchParams.set('email', email) + + return { + from, + to: email, + subject, + html: mustache.render( + templates.layout, + { actionUrl, name, nonce, supportUrl, subject }, + { content: templates.passwordReset }, + ), + } +} + +export const wrongAccountTemplate = ({ email }) => { + const subject = 'Falsche Mailadresse? | Wrong E-mail?' + const actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI) + + return { + from, + to: email, + subject, + html: mustache.render( + templates.layout, + { actionUrl, supportUrl }, + { content: templates.wrongAccount }, + ), + } +} diff --git a/backend/src/middleware/email/templates/emailVerification.html b/backend/src/middleware/email/templates/emailVerification.html new file mode 100644 index 000000000..ff8be01f2 --- /dev/null +++ b/backend/src/middleware/email/templates/emailVerification.html @@ -0,0 +1,189 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/backend/src/middleware/email/templates/index.js b/backend/src/middleware/email/templates/index.js new file mode 100644 index 000000000..594cae334 --- /dev/null +++ b/backend/src/middleware/email/templates/index.js @@ -0,0 +1,11 @@ +import fs from 'fs' +import path from 'path' + +const readFile = fileName => fs.readFileSync(path.join(__dirname, fileName), 'utf-8') + +export const signup = readFile('./signup.html') +export const passwordReset = readFile('./resetPassword.html') +export const wrongAccount = readFile('./wrongAccount.html') +export const emailVerification = readFile('./emailVerification.html') + +export const layout = readFile('./layout.html') diff --git a/backend/src/middleware/email/templates/layout.html b/backend/src/middleware/email/templates/layout.html new file mode 100644 index 000000000..014288229 --- /dev/null +++ b/backend/src/middleware/email/templates/layout.html @@ -0,0 +1,256 @@ + + + + + + + + + + {{ subject }} + + + + + + + + + + + + + + + + + + + + +
+ + + + + +
+ + + diff --git a/backend/src/middleware/email/templates/resetPassword.html b/backend/src/middleware/email/templates/resetPassword.html index e0dde53e5..ff8be01f2 100644 --- a/backend/src/middleware/email/templates/resetPassword.html +++ b/backend/src/middleware/email/templates/resetPassword.html @@ -1,448 +1,189 @@ - - + + + + + - - - - - - - Neues Passwort | Reset Password + + + + + - - - - - - - - - - - - - - - - - - - -
- - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - - - - + + + + - + + + + + + +
+ + + +
-

- Human Connection gGmbH
Bahnhofstraße 11, 73235 Weilheim / - Teck
Germany
-

+ style="padding: 20px; padding-top: 0; font-family: Lato, sans-serif; font-size: 16px; line-height: 22px; color: #555555;"> +

+ Hallo {{ name }}!

+

Du hast also dein Passwort vergessen? Kein Problem! Mit Klick auf diesen Button + kannst Du innerhalb der nächsten 24 Stunden Dein Passwort zurücksetzen:

+
+ + + + + +
+ Passwort + zurücksetzen +
+
- +
+ + +
+

Falls Du kein neues Passwort angefordert hast, kannst Du diese E-Mail einfach + ignorieren. Wenn Du noch Fragen hast, melde Dich gerne bei + unserem Support Team!

- - - +
+ + + + + + + +
+

Sollte der Button für Dich nicht funktionieren, kannst Du auch folgenden Code in + Dein Browserfenster kopieren: {{{ nonce }}}

+

Bis bald bei Human Connection!

+

– Dein Human Connection Team

+
+

–––––––––––––––––––––––––––––––––––––––––––––––

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/backend/src/middleware/email/templates/signup.html b/backend/src/middleware/email/templates/signup.html index e4be8c02f..4d875ff57 100644 --- a/backend/src/middleware/email/templates/signup.html +++ b/backend/src/middleware/email/templates/signup.html @@ -1,485 +1,214 @@ - - + + + + + - - - - - - - Willkommen, Bienvenue, Welcome to Human Connection + + + + + - - - - - - - - - - - - - - - - - - - -
- - - -
- Dein Anmeldelink. | Here is your signup link. -
-
- ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ 
-
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - - - - + + + + - + + + + + + +
+ + + + + + +
-

- Human Connection gGmbH
Bahnhofstraße 11, 73235 Weilheim / - Teck
Germany
-

+ style="padding: 20px; padding-top: 0; font-family: Lato, sans-serif; font-size: 16px; line-height: 22px; color: #555555;"> +

+ Willkommen bei Human Connection!

+

Danke, dass Du dich angemeldet hast – wir freuen uns, Dich dabei zu haben. Jetzt + fehlt nur noch eine Kleinigkeit, bevor wir gemeinsam die Welt verbessern können ... Bitte bestätige + Deine E-Mail Adresse:

+
+ + + + + +
+ Bestätige + Deine E-Mail Adresse +
+ +
+

–––––––––––––––––––––––––––––––––––––––––––––––

- +
+ + + + + +
+

Falls Du Dich nicht selbst bei Human Connection angemeldet hast, schau doch mal vorbei! + Wir sind ein gemeinnütziges Aktionsnetzwerk – von Menschen für Menschen.

+

PS: Wenn Du keinen Account bei uns möchtest, kannst Du diese + E-Mail einfach ignorieren. ;)

+
+

–––––––––––––––––––––––––––––––––––––––––––––––

- - - +
+ + + + + + + +
+

Melde Dich gerne bei + unserem Support Team, wenn Du Fragen hast.

+

Bis bald bei Human Connection!

+

– Dein Human Connection Team

+
+

–––––––––––––––––––––––––––––––––––––––––––––––

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/backend/src/middleware/email/templates/templateBuilder.js b/backend/src/middleware/email/templates/templateBuilder.js deleted file mode 100644 index fdeb47a89..000000000 --- a/backend/src/middleware/email/templates/templateBuilder.js +++ /dev/null @@ -1,48 +0,0 @@ -import fs from 'fs' -import path from 'path' -import mustache from 'mustache' -import CONFIG from '../../../config' - -const from = '"Human Connection" ' -const supportUrl = 'https://human-connection.org/en/contact' - -const signupHtml = fs.readFileSync(path.join(__dirname, './signup.html'), 'utf-8') -const passwordResetHtml = fs.readFileSync(path.join(__dirname, './resetPassword.html'), 'utf-8') -const wrongAccountHtml = fs.readFileSync(path.join(__dirname, './wrongAccount.html'), 'utf-8') - -export const signupTemplate = ({ email, nonce }) => { - const actionUrl = new URL('/registration/create-user-account', CONFIG.CLIENT_URI) - actionUrl.searchParams.set('nonce', nonce) - actionUrl.searchParams.set('email', email) - - return { - from, - to: email, - subject: 'Willkommen, Bienvenue, Welcome to Human Connection!', - html: mustache.render(signupHtml, { actionUrl, supportUrl }), - } -} - -export const resetPasswordTemplate = ({ email, nonce, name }) => { - const actionUrl = new URL('/password-reset/change-password', CONFIG.CLIENT_URI) - actionUrl.searchParams.set('nonce', nonce) - actionUrl.searchParams.set('email', email) - - return { - from, - to: email, - subject: 'Neues Passwort | Reset Password', - html: mustache.render(passwordResetHtml, { actionUrl, name, nonce, supportUrl }), - } -} - -export const wrongAccountTemplate = ({ email }) => { - const actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI) - - return { - from, - to: email, - subject: 'Falsche Mailadresse? | Wrong E-mail?', - html: mustache.render(wrongAccountHtml, { actionUrl, supportUrl }), - } -} diff --git a/backend/src/middleware/email/templates/wrongAccount.html b/backend/src/middleware/email/templates/wrongAccount.html index b8e6f6f57..3cea69c4f 100644 --- a/backend/src/middleware/email/templates/wrongAccount.html +++ b/backend/src/middleware/email/templates/wrongAccount.html @@ -1,448 +1,189 @@ - - + + + + + - - - - - - - Falsche Mailadresse? | Wrong E-mail? + + + + + - - - - - - - - - - - - - - - - - - - -
- - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - - - - + + + + - + + + + + + +
+ + + +
-

- Human Connection gGmbH
Bahnhofstraße 11, 73235 Weilheim / - Teck
Germany
-

+ style="padding: 20px; padding-top: 0; font-family: Lato, sans-serif; font-size: 16px; line-height: 22px; color: #555555;"> +

+ Hallo!

+

Du hast bei uns ein neues Password angefordert – leider haben wir aber keinen + Account mit Deiner E-Mailadresse gefunden. Kann es sein, dass Du mit einer anderen Adresse bei uns + angemeldet bist?

+
+ + + + + +
+ Versuch' + es mit einer anderen E-Mail +
+
- +
+ + +
+

Wenn Du noch keinen Account bei Human Connection hast oder Dein Password gar nicht ändern willst, + kannst Du diese E-Mail einfach ignorieren!

- - - +
+ + + + + + + +
+

Ansonsten hilft Dir unser + Support Team gerne weiter.

+

Bis bald bei Human Connection!

+

– Dein Human Connection Team

+
+

–––––––––––––––––––––––––––––––––––––––––––––––

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +