diff options
| author | Adam Soltys <[email protected]> | 2017-04-09 22:23:55 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2017-04-09 22:23:55 -0700 |
| commit | 5e9334ec60e75b3ee3bc539eedc53fa4e0366e29 (patch) | |
| tree | bb3954df085a9ae9471a2e4494c60ea03d5ea061 | |
| parent | 934cab2a4aebbc85e136cafded24acbf2f20d49a (diff) | |
| download | coinos-server-5e9334ec60e75b3ee3bc539eedc53fa4e0366e29.tar.gz coinos-server-5e9334ec60e75b3ee3bc539eedc53fa4e0366e29.zip | |
clean up user routes
| -rw-r--r-- | app.js | 25 | ||||
| -rw-r--r-- | routes/users.js | 469 | ||||
| -rw-r--r-- | views/mail.html | 9 | ||||
| -rw-r--r-- | views/payment.html | 1 | ||||
| -rw-r--r-- | views/users/welcome.html | 13 |
5 files changed, 188 insertions, 329 deletions
@@ -101,8 +101,6 @@ return setTimeout(fetchRates, 120000) })() - app.get('/', cache, sessions["new"]) - app.get('/address', cache, function(req, res) { return res.render('address', { layout: 'layout', @@ -139,38 +137,15 @@ return res.end() }) - app.get('/tips', cache, function(req, res) { - return res.render('tips', { - notice: true, - layout: 'layout', - js: (function() { - return global.js - }), - css: (function() { - return global.css - }) - }) - }) - - app.get('/login', cache, sessions["new"]) - app.post('/login', sessions.create) - app.get('/logout', sessions.destroy) app.get('/users.json', users.index) - app.get('/register', cache, users["new"]) - app.get('/users/new', cache, users["new"]) app.post('/users', users.create) app.get('/verify/:token', users.verify) app.post('/:user', authorize, users.update) - app.get('/:user/edit', authorize, users.edit) - app.get('/:user/profile', authorize, users.profile) - app.get('/:user/wallet', authorize, users.wallet) app.get('/:user/transactions.json', authorize, transactions.json) app.post('/:user/transactions', transactions.create) app.post('/transactions/:txid', transactions.update) app["delete"]('/:user/transactions/:txid', transactions["delete"]) - app.get('/:user/report', authorize, transactions.index) app.get('/:user.json', users.json) - app.get('/:user', cache, users.show) app.use(function(err, req, res, next) { res.status(500) diff --git a/routes/users.js b/routes/users.js index 56db583d..5968a54c 100644 --- a/routes/users.js +++ b/routes/users.js @@ -1,319 +1,180 @@ -(function() { - var Promise, bcrypt, config, db, fs, request; +const Promise = require('bluebird') +const db = require("../redis") +const config = require("../config") +const bcrypt = require('bcryptjs') +const fs = require('fs') +const request = require('request') - Promise = require('bluebird'); - - db = require("../redis"); - - config = require("../config"); - - bcrypt = require('bcryptjs'); - - fs = require('fs'); +module.exports = (sessions) => { + return { + exists: function(req, res) { + return db.hgetall("user:" + req.params.user.toLowerCase(), function(err, obj) { + if (obj != null) { + res.write('true') + } else { + res.write('false') + } + return res.end() + }) + }, + index: function(req, res) { + var result + result = { + 'users': [] + } + return db.keysAsync("user:*").then(function(users) { + return Promise.all(users.map(function(key) { + return db.hgetallAsync(key).then(function(user) { + delete user['password'] + return result.users.push(user) + }) + })) + }).then(function() { + res.status(200).json(JSON.stringify(result)) + }) + }, + json: function(req, res) { + if (!req.params.user) { + res.end() + } + return db.llen((req.params.user.toLowerCase()) + ":transactions", function(err, len) { + return db.hgetall("user:" + (req.params.user.toLowerCase()), function(err, obj) { + delete obj['password'] + obj['index'] = len + res.status(200).json(JSON.stringify(obj)) + return res.end() + }) + }) + }, + create: function(req, res) { + const userkey = "user:" + req.body.username - request = require('request'); + db.hgetallAsync(userkey).then((err, obj) => { + if (obj) { + res.status(400).json({ message: 'User exists' }) + return + } - module.exports = function(sessions) { - return { - exists: function(req, res) { - return db.hgetall("user:" + req.params.user.toLowerCase(), function(err, obj) { - if (obj != null) { - res.write('true'); - } else { - res.write('false'); - } - return res.end(); - }); - }, - index: function(req, res) { - var result; - result = { - 'users': [] - }; - return db.keysAsync("user:*").then(function(users) { - return Promise.all(users.map(function(key) { - return db.hgetallAsync(key).then(function(user) { - delete user['password']; - return result.users.push(user); - }); - })); - }).then(function() { - res.writeHead(200, { - "Content-Type": "application/json" - }); - res.write(JSON.stringify(result)); - return res.end(); - }); - }, - json: function(req, res) { - if (!req.params.user) { - res.end(); + if (req.body.passconfirm !== req.body.password) { + res.status(400).json({ message: 'Passwords must match' }) + return } - return db.llen((req.params.user.toLowerCase()) + ":transactions", function(err, len) { - return db.hgetall("user:" + (req.params.user.toLowerCase()), function(err, obj) { - delete obj['password']; - obj['index'] = len; - res.writeHead(200, { - "Content-Type": "application/json" - }); - res.write(JSON.stringify(obj)); - return res.end(); - }); - }); - }, - show: function(req, res) { - return db.hgetall("user:" + req.params.user.toLowerCase(), function(err, obj) { - var ext, options, path; - if (obj) { - options = { - user: req.params.user.toLowerCase(), - layout: 'layout', - navigation: true, - js: (function() { - return global.js; - }), - css: (function() { - return global.css; - }) - }; - if (req.query.verified != null) { - options.verified = true; - } - if (obj.logo && obj.logo.length > 3) { - ext = obj.logo.substr(obj.logo.length - 3); - path = "public/img/logos/" + obj.username + "." + ext; - fs.lstat(path, function(err, stats) { - if ((ext === 'jpg' || ext === 'png' || ext === 'gif') && (err || !stats.isFile())) { - try { - return request("" + obj.logo).pipe(fs.createWriteStream(path)); - } catch (undefined) {} - } - }); - } - res.render('users/show', options); - return delete req.session.verified; - } else { - return res.render('sessions/new', { - notice: true, - layout: 'layout', - js: (function() { - return global.js; - }), - css: (function() { - return global.css; + + bcrypt.hash(req.body.password, 12, function(err, hash) { + db.sadd("users", userkey) + db.hmset(userkey, { + username: req.body.username, + password: hash, + email: req.body.email, + commission: req.body.commission || '', + unit: req.body.unit || 'CAD', + pubkey: req.body.pubkey || '', + privkey: req.body.privkey || '' + }, function() { + res.status(200).json({ message: 'User added successfully' }) + require('crypto').randomBytes(48, function(ex, buf) { + const token = buf.toString('base64').replace(/\//g, '').replace(/\+/g, '') + db.set("token:" + token, req.body.username) + host = req.hostname + if (host === 'localhost') { + host += ':' + req.port + } + url = req.protocol + "://" + host + "/verify/" + token + res.render('users/welcome', { + user: req.body.username.toLowerCase(), + layout: 'mail', + url: url, + privkey: req.body.privkey, + js: (() => global.js), + css: (() => global.css) + }, function(err, html) { + const helper = require('sendgrid').mail + const from_email = new helper.Email('[email protected]') + const to_email = new helper.Email(req.body.email) + const subject = 'Welcome to CoinOS' + const content = new helper.Content('text/html', html) + const mail = new helper.Mail(from_email, subject, to_email, content) + const sg = require('sendgrid')(config.sendgrid_token) + const ereq = sg.emptyRequest({ + method: 'POST', + path: '/v3/mail/send', + body: mail.toJSON() + }) + sg.API(ereq, function(error, response) { + console.log(response.statusCode) + console.log(response.body) + console.log(response.headers) + }) }) - }); - } - }); - }, - "new": function(req, res) { - return res.render('users/new', { - layout: 'layout', - js: (function() { - return global.js; - }), - css: (function() { - return global.css; + }) }) - }); - }, - create: function(req, res) { - var errormsg, userkey; - errormsg = ""; - userkey = "user:" + req.body.username; - return db.hgetall(userkey, function(err, obj) { - if (obj) { - errormsg += "Username exists"; - } - if (req.body.confirm !== req.body.password) { - errormsg += "Passwords must match"; - } - if (errormsg) { - return res.render('users/new', { - layout: 'layout', - js: (function() { - return global.js; - }), - css: (function() { - return global.css; - }), - error: errormsg - }); - } - bcrypt.hash(req.body.password, 12, function(err, hash) { - db.sadd("users", userkey); - return db.hmset(userkey, { - username: req.body.username, - password: hash, - email: req.body.email, - commission: req.body.commission, - unit: req.body.unit, - pubkey: req.body.pubkey, - privkey: req.body.privkey + }) + }) + }, + update: function(req, res) { + if (req.body.password === '') { + delete req.body.password + } + db.hmset("user:" + req.params.user.toLowerCase(), req.body, function() { + req.session.user = req.body + delete req.session.user.password + if (req.body.password != null) { + bcrypt.hash(req.body.password, 12, function(err, password) { + db.hmset("user:" + (req.params.user.toLowerCase()), { + email: email, + password: password, + phone: phone }, function() { - req.session.redirect = "/" + req.body.username + "/edit"; - return sessions.create(req, res); - }); - }); - return require('crypto').randomBytes(48, function(ex, buf) { - var host, token, url; - token = buf.toString('base64').replace(/\//g, '').replace(/\+/g, ''); - db.set("token:" + token, req.body.username); - host = req.hostname; - if (host === 'localhost') { - host += ':3000'; - } - url = req.protocol + "://" + host + "/verify/" + token; - return res.render('users/welcome', { - user: req.body.username.toLowerCase(), - layout: 'mail', - url: url, - privkey: req.body.privkey, - js: (function() { - return global.js; - }), - css: (function() { - return global.css; - }) - }, function(err, html) { - var content, from_email, helper, mail, sg, subject, to_email; - helper = require('sendgrid').mail; - from_email = new helper.Email('[email protected]'); - to_email = new helper.Email(req.body.email); - subject = 'Welcome to CoinOS'; - content = new helper.Content('text/html', html); - mail = new helper.Mail(from_email, subject, to_email, content); - sg = require('sendgrid')(config.sendgrid_token); - request = sg.emptyRequest({ - method: 'POST', - path: '/v3/mail/send', - body: mail.toJSON() - }); - return sg.API(request, function(error, response) { - console.log(response.statusCode); - console.log(response.body); - return console.log(response.headers); - }); - }); - }); - }); - }, - edit: function(req, res) { - return res.render('users/edit', { - user: req.params.user.toLowerCase(), - layout: 'layout', - navigation: true, - js: (function() { - return global.js; - }), - css: (function() { - return global.css; - }) - }); - }, - profile: function(req, res) { - return res.render('users/profile', { - user: req.params.user.toLowerCase(), - layout: 'layout', - navigation: true, - js: (function() { - return global.js; - }), - css: (function() { - return global.css; - }) - }); - }, - update: function(req, res) { - if (req.body.password === '') { - delete req.body.password; - } - db.hmset("user:" + req.params.user.toLowerCase(), req.body, function() { - req.session.user = req.body; - delete req.session.user.password; - if (req.body.password != null) { - return bcrypt.hash(req.body.password, 12, function(err, password) { - return db.hmset("user:" + (req.params.user.toLowerCase()), { - email: email, - password: password, - phone: phone - }, function() { - if (req.xhr) { - res.send({}); - return res.end(); - } else { - return res.redirect("/" + (req.params.user.toLowerCase())); - } - }); - }); - } else { - if (req.xhr) { - res.send({}); - return res.end(); - } else { - return res.redirect("/" + (req.params.user.toLowerCase())); - } - } - }); - if (process.env.NODE_ENV === 'production' && (req.body.privkey != null) && req.body.privkey !== '' && req.body.email !== '') { - return res.render('users/key', { - user: req.params.user.toLowerCase(), - layout: 'mail', - key: req.body.privkey, - js: (function() { - return global.js; - }), - css: (function() { - return global.css; + res.status(200).json({ message: 'User updated' }) }) - }, function(err, html) { - var content, from_email, helper, mail, sg, subject, to_email; - helper = require('sendgrid').mail; - from_email = new helper.Email('[email protected]'); - to_email = new helper.Email(req.body.email); - subject = 'CoinOS Wallet Key'; - content = new helper.Content('text/html', html); - mail = new helper.Mail(from_email, subject, to_email, content); - sg = require('sendgrid')(config.sendgrid_token); - request = sg.emptyRequest({ - method: 'POST', - path: '/v3/mail/send', - body: mail.toJSON() - }); - return sg.API(request, function(error, response) { - console.log(response.statusCode); - console.log(response.body); - return console.log(response.headers); - }); - }); + }) + } else { + res.status(200).json({ message: 'User updated' }) } - }, - verify: function(req, res) { - return db.get("token:" + req.params.token, function(err, reply) { - if (err || !reply) { - res.write("Invalid Verification Token"); - return res.end(); - } else { - return db.hset("user:" + (reply.toString()), "verified", "true", function() { - return res.redirect("/" + (reply.toString()) + "?verified"); - }); - } - }); - }, - wallet: function(req, res) { - return res.render('users/wallet', { + }) + if (process.env.NODE_ENV === 'production' && (req.body.privkey != null) && req.body.privkey !== '' && req.body.email !== '') { + return res.render('users/key', { user: req.params.user.toLowerCase(), - layout: 'layout', - navigation: true, + layout: 'mail', + key: req.body.privkey, js: (function() { - return global.js; + return global.js }), css: (function() { - return global.css; + return global.css + }) + }, function(err, html) { + const helper = require('sendgrid').mail + const from_email = new helper.Email('[email protected]') + const to_email = new helper.Email(req.body.email) + const subject = 'CoinOS Wallet Key' + const content = new helper.Content('text/html', html) + const mail = new helper.Mail(from_email, subject, to_email, content) + const sg = require('sendgrid')(config.sendgrid_token) + const ereq = sg.emptyRequest({ + method: 'POST', + path: '/v3/mail/send', + body: mail.toJSON() }) - }); + sg.API(ereq, function(error, response) { + console.log(response.statusCode) + console.log(response.body) + console.log(response.headers) + }) + }) } - }; - }; - -}).call(this); + }, + verify: function(req, res) { + return db.get("token:" + req.params.token, function(err, reply) { + if (err || !reply) { + res.write("Invalid Verification Token") + return res.end() + } else { + db.hset("user:" + (reply.toString()), "verified", "true", function() { + res.status(200) + }) + } + }) + }, + } +} diff --git a/views/mail.html b/views/mail.html new file mode 100644 index 00000000..ca91608a --- /dev/null +++ b/views/mail.html @@ -0,0 +1,9 @@ +<html> + <head> + <meta charset="utf-8" /> + <style> + body { font-family: "Helvetica Neue",Helvetica,Arial,sans-serif"; } + </style> + </head> + <body>{{{yield}}}</body> +</html> diff --git a/views/payment.html b/views/payment.html new file mode 100644 index 00000000..c18f0a13 --- /dev/null +++ b/views/payment.html @@ -0,0 +1 @@ +You received a payment of {{value}} BTC diff --git a/views/users/welcome.html b/views/users/welcome.html new file mode 100644 index 00000000..00c67ebd --- /dev/null +++ b/views/users/welcome.html @@ -0,0 +1,13 @@ +<h1>Welcome to CoinOS</h1> + +<p>Thanks for signing up! Please verify your email by clicking this link:<p> + +<div class="well"><a href="{{url}}">{{url}}</a></div> + +<h2>Your Bitcoin Wallet</h2> + +<p>We've generated a Bitcoin wallet key for you to receive payments to. This is your private key, encrypted with your account password:</p> + +<pre>{{privkey}}</pre> + +<p>You will need this key, along with your password, to access funds that you receive. We can't recover your password for you if you forget it, so keep it written down somewhere safe.</p> |
