hostr/api/routes/user.js

91 lines
2.8 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';
2016-06-19 10:14:47 -07:00
import models from '../../models';
2015-07-09 23:01:43 +01:00
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() {
2016-06-19 10:14:47 -07:00
const transactions = yield models.transaction.findAll({ userId: this.user.id });
2015-07-09 23:01:43 +01:00
2016-06-19 10:14:47 -07:00
this.body = transactions.map((item) => {
2015-07-09 23:01:43 +01:00
return {
2016-06-19 10:14:47 -07:00
id: item.id,
amount: item.amount / 100,
date: item.date,
description: item.description,
type: 'direct',
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}}');
2016-06-19 10:14:47 -07:00
const user = yield models.user.findById(this.user.id);
this.assert(yield passwords.match(this.request.body.current_password, user.password), 400,
2016-06-06 15:37:00 +01:00
'{"error": {"message": "Incorrect password", "code": 606}}');
2015-07-09 23:01:43 +01:00
if (this.request.body.email && this.request.body.email !== user.email) {
2016-06-19 10:14:47 -07:00
user.email = this.request.body.email;
2015-07-09 23:01:43 +01:00
}
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}}');
2016-06-19 10:14:47 -07:00
user.password = yield passwords.hash(this.request.body.new_password);
2015-07-09 23:01:43 +01:00
}
2016-06-19 10:14:47 -07:00
yield user.save();
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();
});
}