From a73481af8b7472c53ceda548d2cfc574d438f9d5 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 23 Apr 2013 15:39:23 -0400 Subject: [PATCH] more work with reset page --- config.js | 3 ++- package.json | 7 +++--- src/user.js | 54 ++++++++++++++++++++++++++++++++++++++--------- src/webserver.js | 10 ++++++--- src/websockets.js | 4 +--- utils.js | 10 +++++++++ 6 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 utils.js diff --git a/config.js b/config.js index ac009fe563..50a77c6f42 100644 --- a/config.js +++ b/config.js @@ -1,9 +1,10 @@ var config = { + "url": "http://designcreateplay.com:4567/", "port": 4567, "mailer": { host: 'localhost', port: '25', - from: 'mailer@localhost.lan' + from: 'mailer@designcreateplay.com' } } diff --git a/package.json b/package.json index 529a3439b6..0a87afb1a6 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,19 @@ { - "author": "psychobunny ", + "author": "psychobunny , julianlam ", "name": "nodeforum", "description": "nodeforum dev", "version": "0.0.1", "homepage": "http://www.designcreateplay.com", "repository": { - "url": "" + "url": "https://github.com/psychobunny/node-forum/" }, "main": "app.js", "dependencies": { "socket.io": "0.9.14", "redis": "0.8.3", "express": "3.2.0", - "connect": "2.7.6" + "connect": "2.7.6", + "emailjs": "0.3.4" }, "devDependencies": {}, "optionalDependencies": {}, diff --git a/src/user.js b/src/user.js index c1e2551606..b98094c58b 100644 --- a/src/user.js +++ b/src/user.js @@ -1,4 +1,8 @@ -var RDB = require('./redis.js'); +var config = require('../config.js'), + utils = require('../utils.js'), + RDB = require('./redis.js'), + emailjs = require('emailjs'), + emailjsServer = emailjs.server.connect(config.mailer); (function(User) { var current_uid; @@ -24,7 +28,6 @@ var RDB = require('./redis.js'); if (user.password != password) { return global.socket.emit('user.login', {'status': 0, 'message': 'Incorrect username / password combination.'}); } else { - console.log('in'); return global.socket.emit('user.login', {'status': 1, 'message': 'Logged in!'}); } }); @@ -95,13 +98,45 @@ var RDB = require('./redis.js'); RDB.get('username:' + username + ':uid', callback); }; + User.get_uid_by_email = function(email, callback) { + RDB.get('email:' + email, callback) + }; + User.send_reset = function(email) { - User.email.exists(email, function(exists) { - if (exists) { - global.socket.emit('user.send_reset', { - status: "ok", - message: "code-sent", - email: email + User.get_uid_by_email(email, function(uid) { + if (uid !== null) { + // Generate a new reset code + var reset_code = utils.generateUUID(); + RDB.set('user:reset:' + reset_code, uid); + + var message = emailjs.message.create({ + text: "Hello,\n\n" + + "We received a request to reset your password, possibly because you have forgotten it. If this is not the case, please ignore this email.\n\n" + + "To continue with the password reset, please click on the following link:\n\n" + + "  " + config.url + 'reset/' + reset_code + "\n\n\n" + + "Thanks!\nNodeBB", + from: config.mailer.from, + to: email, + subject: 'Password Reset Requested', + attachment: [ + { + data: "

Hello,

" + + "

We received a request to reset your password, possibly because you have forgotten it. If this is not the case, please ignore this email.

" + + "

To continue with the password reset, please click on the following link:

" + + "
" + config.url + 'reset/' + reset_code + "
" + + "

Thanks!
NodeBB", + alternative: true + } + ] + }); + emailjsServer.send(message, function(err, success) { + if (err === null) { + global.socket.emit('user.send_reset', { + status: "ok", + message: "code-sent", + email: email + }); + } }); } else { global.socket.emit('user.send_reset', { @@ -115,8 +150,7 @@ var RDB = require('./redis.js'); User.email = { exists: function(email, callback) { - RDB.get('email:' + email, function(exists) { - console.log('email:' + email, exists); + User.get_uid_by_email(email, function(exists) { exists = !!exists; if (typeof callback !== 'function') global.socket.emit('user.email.exists', { exists: exists }); else callback(exists); diff --git a/src/webserver.js b/src/webserver.js index 9e0361c677..44064f99ae 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -26,10 +26,14 @@ var express = require('express'), module.exports.init = function() { // todo move some of this stuff into config.json app.configure(function() { - app.use(express.favicon()); - app.use(express.bodyParser()); - app.use(express.cookieParser()); + app.use(express.favicon()); // 2 args: string path and object options (i.e. expire time etc) + app.use(express.bodyParser()); // Puts POST vars in request.body + app.use(express.cookieParser()); // Presumably important + + // Dunno wtf this does // app.use(express.logger({ format: '\x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms' })); + + // Useful if you want to use app.put and app.delete (instead of app.post all the time) // app.use(express.methodOverride()); app.use(express.static(global.configuration.ROOT_DIRECTORY + '/public')); }); diff --git a/src/websockets.js b/src/websockets.js index 6769704f6c..e27c806f45 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -1,6 +1,4 @@ - -var SocketIO = require('socket.io').listen(global.server); - +var SocketIO = require('socket.io').listen(global.server); (function(io) { var modules = null; diff --git a/utils.js b/utils.js new file mode 100644 index 0000000000..7b003e466a --- /dev/null +++ b/utils.js @@ -0,0 +1,10 @@ +var utils = { + generateUUID: function() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); + return v.toString(16); + }); + } +} + +module.exports = utils; \ No newline at end of file