hostr/api/routes/user.js

97 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-07-09 23:01:43 +01:00
import uuid from 'node-uuid';
2015-09-01 14:09:52 +02:00
import redis from 'redis';
2015-07-09 23:01:43 +01:00
import co from 'co';
import passwords from 'passwords';
import debugname from 'debug';
2015-08-08 20:37:49 +01:00
const debug = debugname('hostr-api:user');
2015-07-09 23:01:43 +01:00
2015-08-30 18:35:05 +02:00
const redisUrl = process.env.REDIS_URL;
2015-07-09 23:01:43 +01:00
2015-08-23 22:12:32 +01:00
export function* get() {
2015-07-09 23:01:43 +01:00
this.body = this.user;
}
2015-08-23 22:12:32 +01:00
export function* token() {
2015-07-09 23:01:43 +01:00
const token = uuid.v4(); // eslint-disable-line no-shadow
yield this.redis.set(token, this.user.id, 'EX', 86400);
2016-06-06 15:37:00 +01:00
this.body = { token };
2015-07-09 23:01:43 +01:00
}
2015-08-23 22:12:32 +01:00
export function* transaction() {
2015-07-09 23:01:43 +01:00
const Transactions = this.db.Transactions;
2016-06-06 15:37:00 +01:00
const transactions = yield Transactions.find({ user_id: this.user.id }).toArray();
2015-07-09 23:01:43 +01:00
2015-08-23 22:12:32 +01:00
this.body = transactions.map((transaction) => { // eslint-disable-line no-shadow
2015-07-09 23:01:43 +01:00
const type = transaction.paypal ? 'paypal' : 'direct';
return {
id: transaction._id,
amount: transaction.paypal ? transaction.amount : transaction.amount / 100,
date: transaction.date,
description: transaction.desc,
2016-06-06 15:37:00 +01:00
type,
2015-07-09 23:01:43 +01:00
};
});
}
export function* settings() {
2016-06-06 15:37:00 +01:00
this.assert(this.request.body, 400,
'{"error": {"message": "Current Password required to update account.", "code": 612}}');
this.assert(this.request.body.current_password, 400,
'{"error": {"message": "Current Password required to update account.", "code": 612}}');
2015-07-09 23:01:43 +01:00
const Users = this.db.Users;
2016-06-06 15:37:00 +01:00
const user = yield Users.findOne({ _id: this.user.id });
this.assert(yield passwords.match(this.request.body.current_password, user.salted_password), 400,
'{"error": {"message": "Incorrect password", "code": 606}}');
2015-07-09 23:01:43 +01:00
const data = {};
if (this.request.body.email && this.request.body.email !== user.email) {
data.email = this.request.body.email;
if (!user.activated_email) {
data.activated_email = user.email; // eslint-disable-line camelcase
}
}
if (this.request.body.new_password) {
2016-06-06 15:37:00 +01:00
this.assert(this.request.body.new_password.length >= 7, 400,
'{"error": {"message": "Password must be 7 or more characters long.", "code": 606}}');
data.salted_password = yield passwords.hash(this.request.body.new_password);
2015-07-09 23:01:43 +01:00
}
2016-06-06 15:37:00 +01:00
Users.updateOne({ _id: user._id }, { $set: data });
2015-07-09 23:01:43 +01:00
this.body = {};
}
export function* events() {
2015-09-01 14:09:52 +02:00
const pubsub = redis.createClient(redisUrl);
2015-08-08 20:37:49 +01:00
pubsub.on('message', (channel, message) => {
2015-07-09 23:01:43 +01:00
this.websocket.send(message);
2015-08-08 20:37:49 +01:00
});
pubsub.on('ready', () => {
2015-08-23 22:12:32 +01:00
this.websocket.on('message', co.wrap(function* wsMessage(message) {
2015-07-09 23:01:43 +01:00
let json;
2015-08-23 22:12:32 +01:00
try {
2015-07-09 23:01:43 +01:00
json = JSON.parse(message);
2016-06-06 15:37:00 +01:00
} catch (err) {
2015-07-09 23:01:43 +01:00
debug('Invalid JSON for socket auth');
this.websocket.send('Invalid authentication message. Bad JSON?');
2015-08-23 01:05:20 +01:00
this.raven.captureError(err);
2015-07-09 23:01:43 +01:00
}
2015-08-23 01:05:20 +01:00
try {
const reply = yield this.redis.get(json.authorization);
if (reply) {
2016-06-06 15:37:00 +01:00
pubsub.subscribe(`/user/${reply}`);
2015-08-23 01:05:20 +01:00
this.websocket.send('{"status":"active"}');
debug('Subscribed to: /user/%s', reply);
} else {
this.websocket.send('Invalid authentication token.');
}
2016-06-06 15:37:00 +01:00
} catch (err) {
2015-08-23 01:05:20 +01:00
debug(err);
this.raven.captureError(err);
2015-07-09 23:01:43 +01:00
}
2015-08-08 20:37:49 +01:00
}.bind(this)));
});
this.websocket.on('close', () => {
2015-07-09 23:01:43 +01:00
debug('Socket closed');
pubsub.quit();
});
}