Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module.exports = function(grunt) {
'dist/loopback.js': ['index.js'],
},
options: {
ignore: ['nodemailer', 'passport', 'bcrypt'],
ignore: ['nodemailer', 'email-templates', 'passport', 'bcrypt'],
standalone: 'loopback'
}
}
Expand Down Expand Up @@ -186,6 +186,7 @@ module.exports = function(grunt) {
// extensions: ['.coffee'],
ignore: [
'nodemailer',
'email-templates',
'passport',
'passport-local',
'superagent',
Expand Down
51 changes: 28 additions & 23 deletions common/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var loopback = require('../../lib/loopback');
var path = require('path');
var SALT_WORK_FACTOR = 10;
var crypto = require('crypto');
var url = require('url');

var bcrypt;
try {
Expand Down Expand Up @@ -305,7 +306,7 @@ module.exports = function(User) {
* var options = {
* type: 'email',
* to: user.email,
* template: 'verify.ejs',
* template: 'verify',
* redirect: '/',
* tokenGenerator: function (user, cb) { cb("random-token"); }
* };
Expand Down Expand Up @@ -341,28 +342,26 @@ module.exports = function(User) {
assert(options.to || this.email, 'Must include options.to when calling user.verify() or the user must have an email property');
assert(options.from, 'Must include options.from when calling user.verify() or the user must have an email property');

var app = userModel.app;
var template = options.template || 'verify';
options.redirect = options.redirect || '/';
options.template = path.resolve(options.template || path.join(__dirname, '..', '..', 'templates', 'verify.ejs'));
options.cache = options.cache || true; // instruct the template engine to cache the template
options.user = this;
options.protocol = options.protocol || 'http';
options.to = options.to || this.email; // we want the actual email address available in the template options
options.protocol = options.protocol || (app && app.get('protocol')) || 'http';

var app = userModel.app;
options.host = options.host || (app && app.get('host')) || 'localhost';
options.port = options.port || (app && app.get('port')) || 3000;
options.restApiRoot = options.restApiRoot || (app && app.get('restApiRoot')) || '/api';
options.verifyHref = options.verifyHref ||
options.protocol +
'://' +
options.host +
':' +
options.port +
options.restApiRoot +
userModel.http.path +
userModel.sharedClass.find('confirm', true).http.path +
'?uid=' +
options.user.id +
'&redirect=' +
options.redirect;
options.verifyHref = options.verifyHref || url.format({
protocol : options.protocol,
hostname : options.host,
port : options.port,
pathname : options.restApiRoot +
userModel.http.path +
userModel.sharedClass.find('confirm', true).http.path,
query : { 'uid' : options.user.id, 'redirect' : options.redirect }
});

// Email model
var Email = options.mailer || this.constructor.email || registry.getModelByType(loopback.Email);
Expand All @@ -375,17 +374,23 @@ module.exports = function(User) {

user.verificationToken = token;
user.save(function(err) {
if (err) {
fn(err);
} else {
sendEmail(user);
if (err) { return fn(err); }
// TODO - support more verification types
switch (options.type) {
case 'email':
/* falls through */
default:
sendEmail(user);
break;
}
});
});

// TODO - support more verification types
function sendEmail(user) {
options.verifyHref += '&token=' + user.verificationToken;
var urlObj = url.parse(options.verifyHref, true);
urlObj.query.token = user.verificationToken;
delete urlObj.search; // otherwise search will be used in place of query
options.verifyHref = url.format(urlObj);

options.text = options.text || 'Please verify your email by opening this link in a web browser:\n\t{href}';

Expand Down
54 changes: 54 additions & 0 deletions lib/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* This file exports methods and objects for working with emails
* and email templates
*
* It is an internal file that should not be used outside of loopback.
* All exported entities can be accessed via the `loopback` object.
* @private
*/

var debug = require('debug')('loopback:email');
var path = require('path');
var emailTemplates = require('email-templates');
var fs = require('fs');

// look in the developers directory before falling back to our own
var templatesDir = path.resolve(process.cwd(), 'templates');
fs.realpath(templatesDir, {}, function(err, resolvedPath) {
if (err) {
debug('Email templates directory not found', err.path);
templatesDir = path.resolve(__dirname, '..', 'templates');
} else {
templatesDir = resolvedPath;
}
debug('Templates directory', templatesDir);
});

var email = exports;

/**
* Lazy load a template helper
*
* loopback.template('foo', {foo: 'bar'}, function (err, html, text) {
* Email.send({ to: 'clarkbw@example.com', html: html, text: text });
* });
*
* @param {String} view Name of the template directory, html.ejs is a required file within named directory.
* @options {Object} options passed to the template for rendering
* @property {Boolean} cache True instructs the rendering engine to cache the template, use in production
* @param {Function} next Called after the template has been rendered.
*/

var _template = null;

email.template = function(view, options, next) {
if (_template) {
_template(view, options, next);
} else {
emailTemplates(templatesDir, function(err, template) {
if (err || !template) { console.error(err); }
_template = template;
return email.template(view, options, next);
});
}
};
20 changes: 3 additions & 17 deletions lib/loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var express = require('express');
var loopbackExpress = require('./server-app');
var proto = require('./application');
var fs = require('fs');
var ejs = require('ejs');
var path = require('path');
var merge = require('util')._extend;
var assert = require('assert');
Expand Down Expand Up @@ -118,6 +117,9 @@ function mixin(source) {
}

mixin(require('./runtime'));
if (loopback.isServer) {
mixin(require('./email'));
}

/*!
* Expose static express methods like `express.errorHandler`.
Expand Down Expand Up @@ -199,22 +201,6 @@ loopback.remoteMethod = function(fn, options) {
fn.http = fn.http || {verb: 'get'};
};

/**
* Create a template helper.
*
* var render = loopback.template('foo.ejs');
* var html = render({foo: 'bar'});
*
* @param {String} path Path to the template file.
* @returns {Function}
*/

loopback.template = function(file) {
var templates = this._templates || (this._templates = {});
var str = templates[file] || (templates[file] = fs.readFileSync(file, 'utf8'));
return ejs.compile(str);
};

require('../server/current-context')(loopback);

/**
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"debug": "^2.1.2",
"depd": "^1.0.0",
"ejs": "^2.3.1",
"email-templates": "^1.2.1",
"express": "^4.12.2",
"inflection": "^1.6.0",
"loopback-connector-remote": "^1.0.3",
Expand Down Expand Up @@ -96,6 +97,7 @@
"./server/current-context.js": "./browser/current-context.js",
"connect": false,
"nodemailer": false,
"email-templates": false,
"supertest": false,
"depd": "loopback-datasource-juggler/lib/browser.depd.js",
"bcrypt": false
Expand Down
2 changes: 1 addition & 1 deletion templates/verify.ejs → templates/verify/html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

<p>
<a href="<%= verifyHref %>"><%= verifyHref %></a>
</p>
</p>
8 changes: 8 additions & 0 deletions templates/verify/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* these styles are 'juiced' into inline styles for the email */
h1 {
color: #0066dd;
}

p {
margin: 1em auto;
}
1 change: 1 addition & 0 deletions templates/verify/text.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'Please verify your email by opening this link in a web browser:\n\t<%= verifyHref %>'
11 changes: 11 additions & 0 deletions test/email.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,15 @@ describe('Email and SMTP', function() {
});
});
});
describe('Email templates', function() {
beforeEach(function() {
MyEmail = loopback.Email.extend('my-email');
loopback.autoAttach();
});
it('should ', function() {
var connector = new MailConnector({transports: [
{type: 'stub', service: 'gmail'}
]});
});
});
});
1 change: 1 addition & 0 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ module.exports = function(config) {
// extensions: ['.coffee'],
ignore: [
'nodemailer',
'email-templates',
'passport',
'passport-local',
'superagent',
Expand Down