Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom template engine #72

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
Binary file removed .DS_Store
Binary file not shown.
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@ node_modules

# sublime project files
*.sublime-project
*.sublime-workspace
*.sublime-workspace

# Miscellaneous
*~
*#
.DS_STORE
.netbeans
nbproject
.idea
.node_history
.tmp
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ authMethod:[

// object containing template information for the reset emails
template:{
file: string, // the relative path to the `jade` template for the reset emails
engine: 'ejs', // support several render engines, see the `consolidate.js` project
file: string, // the path to the reset email template, relative to the project root path eg: 'views/email.ejs'
vars: object, // object containing any vars you want passed to the template for rendering
}
},
Expand Down Expand Up @@ -69,6 +70,9 @@ if you choose to go with this option then a user upon visiting the url `/auth/re
If you want to take advantage of the built in reset itself have the page you sent your user to above `POST` to `/auth/reset` with the post param of `password` If all is well a password reset will be issued.

## Template
You can customize the email template used in the password reset via the template file defined in `config/waterlock.js` this template file is rendered with the fun and dynamic `jade` markup, the view var `url` is generated and passed to it when a user requests and password reset. You can customize this template to your liking and pass any other view vars you wish to it via the `vars` options in the js file.
You can customize the email template used in the password reset via the template file defined in `config/waterlock.js` this template file is rendered with `ejs`, the sails.js default template engine, the view var `url` is generated and passed to it when a user requests and password reset. You can customize this template to your liking and pass any other view vars you wish to it via the `vars` options in the js file.

### Custom template engine
You can switch to another template engine of choice simply changing the `engine` option within the `waterlock.js` config file. Remember that the template engine must be available inside sails (simply put: install it before switching template engine). See [consolidate.js](https://github.com/tj/consolidate.js/) for a list of supported engines.

Your user can simply try to login to `/login` if the user is not found one will be created using [waterlines](https://github.com/balderdashy/waterline) `findOrCreate` method
Binary file removed lib/.DS_Store
Binary file not shown.
Binary file removed lib/controllers/.DS_Store
Binary file not shown.
27 changes: 14 additions & 13 deletions lib/models/resetToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,22 @@ exports.afterCreate = function(token, cb){
if(config.passwordReset.tokens){

var utils = require('../utils');
var html = utils.getHtmlEmail(token);


// setup e-mail data with unicode symbols
var mailOptions = {
from: config.passwordReset.mail.from, // sender address
subject: config.passwordReset.mail.subject, // Subject line
text: html, // plaintext body
html: html // html body
};

Auth.findOne(token.owner).exec(function(err, u){
mailOptions.to = u.email;

utils.getHtmlEmail(token)
.then(function(html) {
// setup e-mail data with unicode symbols
var mailOptions = {
from: config.passwordReset.mail.from, // sender address
subject: config.passwordReset.mail.subject, // Subject line
text: html, // plaintext body
html: html // html body
};
return [Auth.findOne(token.owner), mailOptions];
})
.spread(function(auth, mailOptions) {
var transport = require('../waterlock-local-auth').transport;
mailOptions.to = auth.email;

transport.sendMail(mailOptions, utils.mailCallback);
});
}
Expand Down
5 changes: 5 additions & 0 deletions lib/templates/views/email.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>A password reset was requested!</h1>
<p>
If this is correct please follow the following link
</p>
<a href="<%= url %>"><%= url %></a>
6 changes: 0 additions & 6 deletions lib/templates/views/email.jade

This file was deleted.

11 changes: 6 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var jade = require('jade');
var consolidate = require('consolidate');
var path = require('path');

/**
* Returns the email jade template as html
* Returns the email template as html, it supports multiple engines with `consolidate.js`
* @param {Token} token
* @return {String} html
*/
Expand All @@ -14,6 +14,7 @@ exports.getHtmlEmail = function(token){
if(typeof config === 'undefined'){
throw new Error('No config file defined, try running [waterlock install config]');
}
var tmplEngine = authConfig.passwordReset.template.engine || 'ejs'; // ejs is the default sails.js template engine

var resetUrl;
if (config.pluralizeEndpoints) {
Expand All @@ -26,10 +27,10 @@ exports.getHtmlEmail = function(token){
var viewVars = authConfig.passwordReset.template.vars;
viewVars.url = resetUrl;

var templatePath = path.normalize(__dirname+'../../../'+authConfig.passwordReset.template.file);
var html = jade.renderFile(templatePath, viewVars);
var templatePath = path.resolve( __dirname, '../../../' + authConfig.passwordReset.template.file );

return html;

return consolidate[tmplEngine](templatePath, viewVars);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/waterlock-local-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ exports.authType = 'local';
* [installPath description]
* @type {[type]}
*/
exports.installPath = path.normalize(__dirname+'/../../../');
exports.installPath = path.resolve(__dirname+'/../../../');

/**
* Conditionally export mail trasport data if
* user has opted for password tokens i.e. password
* resets
*/
var configPath = path.normalize(__dirname+'/../../../config/waterlock.js');
var configPath = path.resolve(__dirname+'/../../../config/waterlock.js');
var wlconfig = require(configPath).waterlock;
var method = {};
if(_.isArray(wlconfig.authMethod)){
Expand Down
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@
"authentication",
"sails"
],
"devDependencies":{
"mocha": "*",
"should": "*",
"proxyquire": "*",
"devDependencies": {
"coveralls": "*",
"ejs": "^2.5.6",
"istanbul": "*",
"jshint": "*"
"jshint": "*",
"mocha": "*",
"proxyquire": "*",
"should": "*"
},
"dependencies":{
"dependencies": {
"bcrypt": "~0.8.5",
"consolidate": "^0.14.5",
"jwt-simple": "~0.3.0",
"lodash": "~3.10.0",
"moment": "~2.10.6",
"nodemailer": "1.4.0",
"jade": "~1.11.0",
"jwt-simple": "~0.3.0",
"node-uuid": "~1.4.2"
"node-uuid": "~1.4.2",
"nodemailer": "1.4.0"
},
"author": "David Rivera <[email protected]>",
"contributors": [
Expand All @@ -47,6 +48,6 @@
"homepage": "https://github.com/davidrivera/waterlock-local-auth",
"preferGlobal": false,
"engines": {
"node": ">=0.10"
"node": ">=4.0.0"
}
}
1 change: 1 addition & 0 deletions test/email.test.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>TEST</h1>
1 change: 0 additions & 1 deletion test/email.test.jade

This file was deleted.

8 changes: 4 additions & 4 deletions test/test_helper.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var proxyquire = require('proxyquire')
, should = require('should')
, path = require('path')
, waterlockPath = path.normalize(__dirname+'/waterlock.js')
, waterlockPath = path.resolve( __dirname + '/waterlock.js' )

var pathStub = {
normalize: function(str){
resolve: function(str){
return waterlockPath;
}
}

exports.waterlock_local = proxyquire.noCallThru().load('../lib/waterlock-local-auth',
{
exports.waterlock_local = proxyquire.noCallThru().load('../lib/waterlock-local-auth',
{
'path': pathStub
});

Expand Down
8 changes: 4 additions & 4 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('utils',function(){
{
'./waterlock-local-auth': waterlock,
'path': {
normalize: function(str) {
return __dirname+"/email.test.jade";
resolve: function(str) {
return __dirname + "/email.test.ejs";
}
}
});
Expand All @@ -20,8 +20,8 @@ describe('utils',function(){
done();
});

it('should return html', function(done){
utils.getHtmlEmail({owner: "test", resetToken: "token"}).should.be.String
it('should return promise with rendered html', function(done){
utils.getHtmlEmail({owner: "test", resetToken: "token"}).should.eventually.be.String
done()
});

Expand Down
23 changes: 12 additions & 11 deletions test/waterlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
*
* defines various options used by waterlock
* for more informaiton checkout
*
*
* http://waterlock.ninja/documentation
*/
module.exports.waterlock = {

// Base URL
//
//
// used by auth methods for callback URI's using oauth and for password
// reset links.
baseUrl: "http://localhost:1337",
// Auth Method(s)
//
// this can be a single string, an object, or an array of objects for your

// Auth Method(s)
//
// this can be a single string, an object, or an array of objects for your
// chosen auth method(s) you will need to see the individual module's README
// file for more information on the attributes necessary. This is an example
// of the local authentication method with password reset tokens disabled.
Expand All @@ -37,9 +37,10 @@ module.exports.waterlock = {
from: "[email protected]",
subject: "Your password reset!",
forwardUrl: "http://localhost:1337"
},
},
template:{
file: "../views/email.jade",
engine: "ejs",
file: "../views/email.ejs",
vars:{}
}
}
Expand All @@ -48,8 +49,8 @@ module.exports.waterlock = {

// JSON Web Tokens
//
// this provides waterlock with basic information to build your tokens,
// these tokens are used for authentication, password reset,
// this provides waterlock with basic information to build your tokens,
// these tokens are used for authentication, password reset,
// and anything else you can imagine
jsonWebTokens:{

Expand Down