Skip to content

Commit

Permalink
adds ability to send email via gmail. #85
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 16, 2015
1 parent 5cdd570 commit 80af307
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 16 deletions.
26 changes: 16 additions & 10 deletions api/handlers/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var bcrypt = require('bcrypt'); // see: https://github.com/nelsonic/bcrypt
var aguid = require('aguid'); // import the module we are using to create (GU)IDs
var JWTSign = require('../lib/auth_jwt_sign.js'); // used to sign JWTS duh.
var transfer = require('../lib/transfer_anon_to_registered.js');
var email = require('../lib/email_welcome');

module.exports = function handler(req, reply) {
var personid = aguid(req.payload.email)
Expand All @@ -30,16 +31,21 @@ module.exports = function handler(req, reply) {

Hoek.assert(res.created, 'Person NOT Registered!'); // only if DB fails!
// transfer any anonymous timers & session to the person
if(req.headers.authorization){
// console.log("AUTH TOKEN:"+req.headers.authorization);
return transfer(req, reply);
}
else {
JWTSign(req, function(token, esres){
return reply(esres).header("Authorization", token);
}); // Asynchronous
}

console.log(' - - - - - - - - - - person')
console.log(person);
console.log(' - - - - - - - - - - email success')
email(person, function(err, eres){
console.log(eres);
if(req.headers.authorization){
// console.log("AUTH TOKEN:"+req.headers.authorization);
return transfer(req, reply);
}
else {
JWTSign(req, function(token, esres){
return reply(esres).header("Authorization", token);
}); // Asynchronous
}
})
}); // end ES.CREATE
}); // end bcrypt.hash
}); // end bcrypt.genSalt
Expand Down
45 changes: 45 additions & 0 deletions api/lib/email_welcome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var nodemailer = require('nodemailer');
var fs = require('fs');
var path = require('path');
var htmlpath = path.resolve(__dirname +'/../templates/welcome_html.html');
var textpath = path.resolve(__dirname +'/../templates/welcome_text.txt');
var template = fs.readFileSync(htmlpath, 'utf8')
var textonly = fs.readFileSync(textpath, 'utf8')
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: process.env.GMAIL_PASSWORD
}
});

// NB! No need to recreate the transporter object. You can use
// the same transporter object for all e-mails

var email = function(person, callback){
var mailOptions = {
from: '#dwyl do what you love! <[email protected]>', // sender address
to: person.email, // list of receivers
subject: 'Welcome to dwyl!', // Subject line
text: textonly, // plaintext body
html: template
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
// console.log(error, info);
callback(error, info)
});
}

var person = {
email : '[email protected]',
name : 'FirstName'
}

// email(person, function(error,info){
// console.log('hello!');
// console.log(info);
// })

module.exports = email;
14 changes: 14 additions & 0 deletions api/templates/welcome_html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p>Hi!</p>

<p>Thanks so much for joining us to alpha test dwyl, we’re excited to have you!</p>

<p>For now, dwyl will help you track your time and soon it will help you do much more, including
<a href="https://github.com/ideaq/start-here/blob/master/manifesto.md">
making time for what you love
<a/>.
</p>

<p>If you have any questions or find any bugs, big or small, please drop us a note or <a href="https://github.com/ideaq/time/labels/MVP">add an issue to github</a>, we <i>really appreciate</i> it!</p>

<p>Happy dwyling!</p>
Ines & Nelson
11 changes: 11 additions & 0 deletions api/templates/welcome_text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Hi!

Thanks so much for joining us to alpha test dwyl, we’re excited to have you!

For now, dwyl will help you track your time and soon it will help you do much more, including making time for what you love. https://github.com/ideaq/start-here/blob/master/manifesto.md

If you have any questions or find any bugs, big or small, please drop us a note or add an issue to github:
https://github.com/ideaq/time/labels/MVP We really appreciate it!

Happy dwyling
Ines & Nelson
2 changes: 1 addition & 1 deletion api/test/anonymous.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test(file + "Anonymous people can create timers!", function(t) {
};
server.inject(options, function(res) {
var T = JSON.parse(res.payload);
t.equal(res.statusCode, 200, "New timer started! " + T.st);
t.equal(res.statusCode, 200, "New timer started! " + T.start);
t.end();
server.stop();
});
Expand Down
1 change: 1 addition & 0 deletions front/public/js/alpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ $(document).ready(function() {
return obj;
}
catch(e){
console.log("DB.GET FAIL: "+key +" | "+value);
console.log(e);
// value is not a stringified object
return value;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dwyl",
"version": "0.1.5",
"version": "0.1.6",
"description": "Track any time-based activity",
"main": "index.js",
"scripts": {
Expand All @@ -10,7 +10,7 @@
"faster": "node ./node_modules/faster/bin/faster.js",
"jshint": "./node_modules/jshint/bin/jshint -c .jshintrc --exclude-path .gitignore .",
"start": "node web.js",
"test": "./node_modules/.bin/istanbul cover ./node_modules/tape/bin/tape ./api/test/*.js | node_modules/tap-spec/bin/cmd.js",
"test": "istanbul cover ./node_modules/tape/bin/tape ./api/test/*.js | node_modules/tap-spec/bin/cmd.js",
"spec": "node ./node_modules/tape/bin/tape ./api/test/*.js | node_modules/tap-spec/bin/cmd.js"
},
"repository": {
Expand Down Expand Up @@ -41,6 +41,7 @@
"joi": "^6.1.0",
"jsonwebtoken": "^5.0.0",
"lout": "^6.2.1",
"nodemailer": "^1.3.4",
"perma": "^2.0.1",
"sad": "^1.0.7"
},
Expand Down
5 changes: 2 additions & 3 deletions web.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ server.register([ {register: Basic}, {register: AuthJWT} ], function (err) {

});

server.start(function(){
console.log('Now Visit: http://' + ip + ':' +port);
});
server.start();
console.log('Now Visit: http://' + ip + ':' +port);

module.exports = server;

0 comments on commit 80af307

Please sign in to comment.