Skip to content

Commit

Permalink
now using node-pony
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Feb 3, 2012
1 parent acae2e3 commit 0b9d312
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
3 changes: 1 addition & 2 deletions README.markdown
Expand Up @@ -54,8 +54,7 @@ Create a new password reset session `forgot` with some options `opts`.
returns the email body as a string.

The rest of the options are passed directly to
the [mailer module](https://github.com/Marak/node_mailer)
with reasonable defaults.
[node-pony](https://github.com/substack/node-pony).

When the user clicks on the uri link `forgot` emits a `"request", req, res`
event.
Expand Down
2 changes: 1 addition & 1 deletion example/web/server.js
Expand Up @@ -4,7 +4,7 @@ var app = express.createServer();
app.use(express.static(__dirname));
app.use(express.bodyParser());

var forgot = require('password-reset')({
var forgot = require('../../')({
uri : 'http://localhost:8080/_password_reset',
from : 'password-robot@localhost',
host : 'localhost', port : 25,
Expand Down
52 changes: 27 additions & 25 deletions index.js
@@ -1,12 +1,20 @@
var mailer = require('mailer');
var url = require('url');
var EventEmitter = require('events').EventEmitter;
var pony = require('pony');
var ent = require('ent');

module.exports = function (opts) {
if (typeof opts === 'string') {
opts = { uri : opts };
}

var send = pony({
host : opts.host || 'localhost',
domain : opts.domain || opts.host || 'localhost',
port : opts.port || 25,
from : opts.from || 'password-robot@localhost',
});

var reset = new Forgot(opts);

var self = function (email, cb) {
Expand All @@ -15,35 +23,29 @@ module.exports = function (opts) {

var uri = session.uri = opts.uri + '?' + session.id;

var body = opts.body
? opts.body(uri, email)
: 'Please click this link to reset your password:\r\n'
+ encodeURI(uri)
;

var msg = {
host : opts.host || 'localhost',
domain : opts.domain || 'localhost',
port : opts.port || 25,
to : [ email ],
from : opts.from || 'password-robot@localhost',
subject : opts.subject || 'password reset confirmation',
body : body,
};
[ 'ssl', 'authentication', 'username', 'password' ]
.forEach(function (name) {
if (opts[name] !== undefined) msg[name] = opts[name];
})
;
mailer.send(msg, function (err) {
send({ to : email }, function (err, req) {
if (err) {
if (cb) cb(err);
session.emit('failure', err);
delete reset.sessions[session.id];
}
else {
if (cb) cb(null);
session.emit('success');
req.url = req.uri = uri;
req.setHeader('content-type', 'text/html');
req.setHeader(
'subject',
opts.subject || 'password reset confirmation'
);
if (cb) cb(null, req)
if (cb && cb.length <= 1) {
req.end([
'Click this link to reset your password:\r\n',
'<br>',
'<a href="' + encodeURI(uri) + '">',
ent.encode(uri),
'</a>',
''
].join('\r\n'));
}
}
});

Expand Down
7 changes: 4 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{
"name" : "password-reset",
"description" : "middleware for password reset emails",
"version" : "0.0.2",
"version" : "0.1.0",
"repository" : {
"type" : "git",
"url" : "git://github.com/substack/node-password-reset.git"
Expand All @@ -18,10 +18,11 @@
"example" : "example"
},
"dependencies" : {
"mailer" : "0.4.52"
"pony" : "~0.0.1"
},
"devDependencies" : {
"express" : "2.5.x"
"express" : "2.5.x",
"smtp-protocol" : "0.1.x"
},
"engines" : {
"node" : ">=0.4.0"
Expand Down

0 comments on commit 0b9d312

Please sign in to comment.