From 4f7d646edd354ffc330cea320257198ec12ed2e6 Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Fri, 27 Oct 2017 19:29:19 -0700 Subject: [PATCH] got email pug templates to work --- .../users.authentication.server.controller.js | 78 ++++++++++++------- app/models/user.server.model.js | 19 ++--- app/views/500.server.view.pug | 6 +- app/views/index.server.view.pug | 65 ++++++++-------- app/views/verification.email.view.html | 65 ---------------- app/views/verification.email.view.pug | 8 +- app/views/welcome.email.view.html | 64 --------------- app/views/welcome.email.view.pug | 6 +- config/express.js | 2 - config/locales/en.json | 6 +- package.json | 6 +- 11 files changed, 103 insertions(+), 222 deletions(-) delete mode 100644 app/views/verification.email.view.html delete mode 100644 app/views/welcome.email.view.html diff --git a/app/controllers/users/users.authentication.server.controller.js b/app/controllers/users/users.authentication.server.controller.js index 91d1d745..541950b8 100755 --- a/app/controllers/users/users.authentication.server.controller.js +++ b/app/controllers/users/users.authentication.server.controller.js @@ -9,16 +9,10 @@ var errorHandler = require('../errors.server.controller'), config = require('../../../config/config'), User = mongoose.model('User'), tokgen = require('../../libs/tokenGenerator'), - fs = require('fs'); - -require.extensions['.html'] = function (module, filename) { - module.exports = fs.readFileSync(filename, 'utf8'); -}; - -var welcomeEmail = require("../../views/welcome.email.view.html"); -var verificationEmail = require("../../views/verification.email.view.html"); - - + fs = require('fs'), + i18n = require('i18n'), + async = require('async'), + pug = require('pug'); var nev = require('email-verification')(mongoose); @@ -26,6 +20,15 @@ var nev = require('email-verification')(mongoose); var config_nev = function () { nev.configure({ + + verifyMailOptions: { + from: config.mailer.from + }, + + confirmMailOptions: { + from: config.mailer.from + }, + persistentUserModel: User, tempUserCollection: config.tempUserCollection, emailAndUsernameUnique: true, @@ -33,19 +36,7 @@ var config_nev = function () { verificationURL: config.baseUrl+'/#!/verify/${URL}', transportOptions: config.mailer.options, - verifyMailOptions: { - from: config.mailer.from, - subject: '✔ Activate your new TellForm account!', - html: verificationEmail, - text: 'Please verify your account by clicking the following link, or by copying and pasting it into your browser: ${URL}' - }, - - confirmMailOptions: { - from: config.mailer.from, - subject: '✔ Welcome to {{app.title}}!', - html: welcomeEmail, - text: 'Your account has been successfully verified.' - }, + verifySendMailCallback: function(err, info) { if (err) { throw err; @@ -63,13 +54,22 @@ var config_nev = function () { throw err; } }); - }; config_nev(); exports.validateVerificationToken = function(req, res){ - nev.confirmTempUser(req.params.token, function(err, user) { + + const fn = pug.compileFile(__dirname + "/../../views/welcome.email.view.pug"); + var renderedHtml = fn(res.locals); + + var emailTemplate = { + subject: i18n.__('WELCOME_EMAIL_SUBJECT', config.app.title), + html: renderedHtml, + text: i18n.__('WELCOME_EMAIL_TEXT') + }; + + nev.confirmTempUser(req.params.token, emailTemplate, function(err, user) { if(err) { return res.status(500).send( {message: err } ); } else if (user){ @@ -83,7 +83,16 @@ exports.validateVerificationToken = function(req, res){ }; exports.resendVerificationEmail = function(req, res, next){ - nev.resendVerificationEmail(req.body.email, function(err, userFound) { + const fn = pug.compileFile(__dirname + "/../../views/verification.email.view.pug"); + var renderedHtml = fn(res.locals); + + var emailTemplate = { + subject: i18n.__('VERIFICATION_EMAIL_SUBJECT'), + html: renderedHtml, + text: i18n.__('VERIFICATION_EMAIL_TEXT') + }; + + nev.resendVerificationEmail(req.body.email, emailTemplate, function(err, userFound) { if(err) { return res.status(500).send( {message: errorHandler.getErrorMessage(err) } ); } @@ -112,6 +121,7 @@ exports.signup = function(req, res) { // Then save the temporary user nev.createTempUser(user, function (err, existingPersistentUser, newTempUser) { if (err) { + console.log(err); return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); @@ -120,10 +130,18 @@ exports.signup = function(req, res) { // new user created if (newTempUser) { - var URL = newTempUser[nev.options.URLFieldName]; - nev.sendVerificationEmail(user.email, URL, function (sendEmailErr, info) { - if (sendEmailErr) { + const fn = pug.compileFile(__dirname + "/../../views/verification.email.view.pug"); + var renderedHtml = fn(res.locals); + var URL = newTempUser[nev.options.URLFieldName]; + var emailTemplate = { + subject: i18n.__('VERIFICATION_EMAIL_SUBJECT'), + html: renderedHtml, + text: i18n.__('VERIFICATION_EMAIL_TEXT') + }; + + nev.sendVerificationEmail(user.email, URL, emailTemplate, function (sendEmailErr, info) { + if (sendEmailErr) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); @@ -131,7 +149,7 @@ exports.signup = function(req, res) { return res.status(200).send('An email has been sent to you. Please check it to verify your account.'); }); } else { - return res.status(400).send({message: 'Error: User already exists!'}); + return res.status(400).send({message: 'User with username/email already exists!'}); } }); }; diff --git a/app/models/user.server.model.js b/app/models/user.server.model.js index 98e2d613..45d3cefc 100755 --- a/app/models/user.server.model.js +++ b/app/models/user.server.model.js @@ -48,7 +48,7 @@ var validateLocalStrategyPassword = function(password) { * A Validation function for username */ var validateUsername = function(username) { - return (username.match(/^[a-zA-Z0-9]+$/) !== null); + return (username.match(/^[a-zA-Z0-9.-_]+$/) !== null); }; @@ -70,22 +70,15 @@ var UserSchema = new Schema({ type: String, trim: true, unique: 'Account already exists with this email', - required: 'Please enter your email', - validate: { - validator: validateLocalStrategyProperty, - message: 'Please fill in your email' - }, - match: [/.+\@.+\..+/, 'Please fill a valid email address'] + match: [/.+\@.+\..+/, 'Please fill a valid email address'], + required: [true, 'Email is required'] }, username: { type: String, unique: true, - required: true, lowercase: true, - validate: { - validator: validateUsername, - message: 'Please use a valid username' - } + match: [/^[a-zA-Z0-9\.\-\_]+$/, 'Username can only contain alphanumeric characters and \'_\', \'-\' and \'.\''], + required: [true, 'Username is required'] }, passwordHash: { type: String, @@ -96,7 +89,6 @@ var UserSchema = new Schema({ }, provider: { type: String, - required: 'Provider is required', default: 'local' }, providerData: {}, @@ -112,7 +104,6 @@ var UserSchema = new Schema({ type: String, enum: ['en', 'fr', 'es', 'it', 'de'], default: 'en', - required: 'User must have a language' }, lastModified: { type: Date diff --git a/app/views/500.server.view.pug b/app/views/500.server.view.pug index 73e42e9a..688a9af0 100644 --- a/app/views/500.server.view.pug +++ b/app/views/500.server.view.pug @@ -5,4 +5,8 @@ block content div.row.valign h3.col-md-12.text-center=__('500_HEADER') div.col-md-4.col-md-offset-4 - div.col-md-12.text-center(style="padding-bottom: 50px;")=__('500_BODY') \ No newline at end of file + if process.env.NODE_ENV == 'development' + div.col-md-12.text-center(style="padding-bottom: 50px;") + | #{error} + else + div.col-md-12.text-center(style="padding-bottom: 50px;")=__('500_BODY') \ No newline at end of file diff --git a/app/views/index.server.view.pug b/app/views/index.server.view.pug index 8aea90e3..f152379f 100644 --- a/app/views/index.server.view.pug +++ b/app/views/index.server.view.pug @@ -5,42 +5,45 @@ block content script(src='/static/lib/file-saver.js/FileSaver.js', type='text/javascript') - //Embedding The User Object - script(type='text/javascript') var user = null; + //Embedding The User Object + script(type='text/javascript'). + var user = null; - //Embedding The signupDisabled Boolean - script(type='text/javascript'). - var signupDisabled = !{signupDisabled}; - var socketPort = false; - var socketUrl = false; - var subdomainsDisabled = !{subdomainsDisabled}; + //Embedding The signupDisabled Boolean + script(type='text/javascript'). + var signupDisabled = !{signupDisabled}; + var socketPort = false; + var socketUrl = false; + var subdomainsDisabled = !{subdomainsDisabled}; - //Embedding socketPort - if socketPort - script(type='text/javascript'). - socketPort = !{socketPort} + //Embedding socketPort + if socketPort + script(type='text/javascript'). + socketPort = !{socketPort} - //Embedding socketUrl - if socketUrl - script(type='text/javascript'). - socketUrl = !{socketUrl} + //Embedding socketUrl + if socketUrl + script(type='text/javascript'). + socketUrl = !{socketUrl} - //Socket.io Client Dependency - script(src='https://cdn.socket.io/socket.io-1.4.5.js') + //Socket.io Client Dependency + script(src='https://cdn.socket.io/socket.io-1.4.5.js') - //Bower JS dependencies - each bowerJSFile in bowerJSFiles - script(type='text/javascript', src=bowerJSFile) - // end Bower JS dependencies + //Bower JS dependencies + each bowerJSFile in bowerJSFiles + script(type='text/javascript', src=bowerJSFile) + // end Bower JS dependencies - script(type='text/javascript', src='https://cdnjs.cloudflare.com/ajax/libs/angular-strap/2.3.8/angular-strap.min.js') + script(type='text/javascript', src='https://cdnjs.cloudflare.com/ajax/libs/angular-strap/2.3.8/angular-strap.min.js') - //Application JavaScript Files - each jsFile in jsFiles - script(type='text/javascript', src=jsFile) - // end Application Javascript dependencies + //Application JavaScript Files + each jsFile in jsFiles + script(type='text/javascript', src=jsFile) + // end Application Javascript dependencies - if process.env.NODE_ENV === 'development' - script(type='text/javascript', src='http://#{request.hostname}:35729/livereload.js') - script(src='https://cdn.ravenjs.com/2.3.0/angular/raven.min.js') - script Raven.config('https://825fefd6b4ed4a4da199c1b832ca845c@sentry.tellform.com/2').install(); \ No newline at end of file + if process.env.NODE_ENV === 'development' + script(type='text/javascript', src='http://#{request.hostname}:35729/livereload.js') + + script(src='https://cdn.ravenjs.com/2.3.0/angular/raven.min.js') + + script Raven.config('https://825fefd6b4ed4a4da199c1b832ca845c@sentry.tellform.com/2').install(); \ No newline at end of file diff --git a/app/views/verification.email.view.html b/app/views/verification.email.view.html deleted file mode 100644 index 97ffbcba..00000000 --- a/app/views/verification.email.view.html +++ /dev/null @@ -1,65 +0,0 @@ - - - @import url(//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&subset=latin,latin-ext); - - - - -
- - - - -
- - - - - - - -
- - - - - - - - - - - - -
-

Hello there!

-

Welcome to TellForm! Here is a special link to activate your new account:

-

Activate my account

-

Thanks so much for using our services! If you have any questions, or suggestions, please feel free to email us here at team@tellform.com.

-

- The TellForm team

-
-
- - - - - - - -
- - - - - -
© TellForm 2017
-
-
-
- - diff --git a/app/views/verification.email.view.pug b/app/views/verification.email.view.pug index 0d99a925..51e31ce9 100644 --- a/app/views/verification.email.view.pug +++ b/app/views/verification.email.view.pug @@ -1,8 +1,6 @@ -doctype html html head style. - @import url(//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&subset=latin,latin-ext); a { color: #007ee6; text-decoration: none; @@ -26,11 +24,11 @@ html td(colspan='3', height='30') tr td(width='36') - td(width='454', align='left', style='color: #444444; border-collapse: collapse; font-size: 11pt; font-family: 'Open Sans', 'Lucida Grande', 'Segoe UI', Arial, Verdana, 'Lucida Sans Unicode', Tahoma, 'Sans Serif'; max-width: 454px', valign='top') + td(width='454', align='left', style="color: #444444; border-collapse: collapse; font-size: 11pt; font-family: 'Open Sans', 'Lucida Grande', 'Segoe UI', Arial, Verdana, 'Lucida Sans Unicode', Tahoma, 'Sans Serif'; max-width: 454px", valign='top') p=__('EMAIL_GREETING') p=__('VERIFICATION_EMAIL_PARAGRAPH_1') p - a(href='https://${URL}')=('VERIFICATION_EMAIL_LINK_TEXT') + a(href='https://${URL}')=__('VERIFICATION_EMAIL_LINK_TEXT') p=__('VERIFICATION_EMAIL_PARAGRAPH_2') a(href='mailto:team@tellform.com') | team@tellform.com @@ -46,6 +44,6 @@ html td(style='padding: 0; border-collapse: collapse') table(cellpadding='0', cellspacing='0', align='center', border='0') tbody - tr(style='color: #c0c0c0; font-size: 11px; font-family: 'Open Sans', 'Lucida Grande', 'Segoe UI', Arial, Verdana, 'Lucida Sans Unicode', Tahoma, 'Sans Serif'; -webkit-text-size-adjust: none') + tr(style="color: #c0c0c0; font-size: 11px; font-family: 'Open Sans', 'Lucida Grande', 'Segoe UI', Arial, Verdana, 'Lucida Sans Unicode', Tahoma, 'Sans Serif'; -webkit-text-size-adjust: none") td(width='400', align='left') td(width='128', align='right') © TellForm 2017 diff --git a/app/views/welcome.email.view.html b/app/views/welcome.email.view.html deleted file mode 100644 index b69702f5..00000000 --- a/app/views/welcome.email.view.html +++ /dev/null @@ -1,64 +0,0 @@ - - - @import url(//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&subset=latin,latin-ext); - - - - -
- - - - -
- - - - - - - -
- - - - - - - - - - - - -
-

Hello there!

-

We would like to welcome you as our newest member!

-

Thanks so much for using TellForm! If you have any questions, or suggestions, please feel free to email us here at team@tellform.com.

-

- The TellForm team

-
-
- - - - - - - -
- - - - - -
© TellForm 2017
-
-
-
- - diff --git a/app/views/welcome.email.view.pug b/app/views/welcome.email.view.pug index e3db6e02..f4562ce0 100644 --- a/app/views/welcome.email.view.pug +++ b/app/views/welcome.email.view.pug @@ -1,8 +1,6 @@ -doctype html html head style. - @import url(//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&subset=latin,latin-ext); a { color: #007ee6; text-decoration: none; @@ -27,7 +25,7 @@ html td(colspan='3', height='30') tr td(width='36') - td(width='454', align='left', style='color: #444444; border-collapse: collapse; font-size: 11pt; font-family: 'Open Sans', 'Lucida Grande', 'Segoe UI', Arial, Verdana, 'Lucida Sans Unicode', Tahoma, 'Sans Serif'; max-width: 454px', valign='top') + td(width='454', align='left', style="color: #444444; border-collapse: collapse; font-size: 11pt; font-family: 'Open Sans', 'Lucida Grande', 'Segoe UI', Arial, Verdana, 'Lucida Sans Unicode', Tahoma, 'Sans Serif'; max-width: 454px", valign='top') p=__('EMAIL_GREETING') p=__('WELCOME_EMAIL_PARAGRAPH_1') p=__('WELCOME_EMAIL_PARAGRAPH_2') @@ -45,6 +43,6 @@ html td(style='padding: 0; border-collapse: collapse') table(cellpadding='0', cellspacing='0', align='center', border='0') tbody - tr(style='color: #c0c0c0; font-size: 11px; font-family: 'Open Sans', 'Lucida Grande', 'Segoe UI', Arial, Verdana, 'Lucida Sans Unicode', Tahoma, 'Sans Serif'; -webkit-text-size-adjust: none') + tr(style="color: #c0c0c0; font-size: 11px; font-family: 'Open Sans', 'Lucida Grande', 'Segoe UI', Arial, Verdana, 'Lucida Sans Unicode', Tahoma, 'Sans Serif'; -webkit-text-size-adjust: none") td(width='400', align='left') td(width='128', align='right') © TellForm 2017 diff --git a/config/express.js b/config/express.js index 705ff479..2b44e8f5 100755 --- a/config/express.js +++ b/config/express.js @@ -162,8 +162,6 @@ module.exports = function(db) { app.use(function(req, res, next) { // express helper for natively supported engines res.locals.__ = res.__ = function() { - console.log('\n\n\n\n\n\nres.locals.__'); - console.log(arguments); return i18n.__.apply(req, arguments); }; diff --git a/config/locales/en.json b/config/locales/en.json index 15b6ce5a..932d0602 100644 --- a/config/locales/en.json +++ b/config/locales/en.json @@ -7,7 +7,11 @@ "VERIFICATION_EMAIL_PARAGRAPH_1": "Welcome to TellForm! Here is a special link to activate your new account:", "VERIFICATION_EMAIL_LINK_TEXT": "Activate my account", "VERIFICATION_EMAIL_PARAGRAPH_2": "Thanks so much for using our services! If you have any questions, or suggestions, please feel free to email us here at", + "VERIFICATION_EMAIL_SUBJECT": "Activate your new TellForm account!", + "VERIFICATION_EMAIL_TEXT": "Please verify your account by clicking the following link, or by copying and pasting it into your browser: ${URL}", "EMAIL_SIGNATURE": "- The TellForm team", "WELCOME_EMAIL_PARAGRAPH_1": "We would like to welcome you as our newest member!", - "WELCOME_EMAIL_PARAGRAPH_2": "We hope you enjoy using TellForm! If you have any trouble please feel free to email us here at" + "WELCOME_EMAIL_PARAGRAPH_2": "We hope you enjoy using TellForm! If you have any trouble please feel free to email us here at", + "WELCOME_EMAIL_SUBJECT": "Welcome to %s!", + "WELCOME_EMAIL_TEXT": "Your account has been successfully verified." } \ No newline at end of file diff --git a/package.json b/package.json index 40860ba2..3700f6b0 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "cookie-parser": "~1.4.0", "deep-diff": "^0.3.4", "dotenv": "^2.0.0", - "email-verification": "~0.4.1", + "email-verification": "github:tellform/node-email-verification", "express": "~4.13.3", "express-session": "~1.12.1", "glob": "^7.0.3", @@ -52,10 +52,6 @@ "grunt-ng-annotate": "~1.0.1", "helmet": "3.5.0", "i18n": "^0.8.3", - "i18n-node": "^2.1.5", - "i18n-x": "^0.1.5", - "i18next": "^10.0.3", - "i18next-express-middleware": "^1.0.7", "jit-grunt": "^0.9.1", "lodash": "^4.17.4", "main-bower-files": "~2.9.0",