hostr/api/routes/user.js

120 lines
3.4 KiB
JavaScript
Raw Permalink 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';
2018-06-02 18:07:00 +00:00
import debugname from 'debug';
2016-06-19 10:14:47 -07:00
import models from '../../models';
2015-07-09 23:01:43 +01:00
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
2018-06-02 15:50:39 +00:00
export async function get(ctx) {
ctx.body = ctx.user;
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
export async function token(ctx) {
2015-07-09 23:01:43 +01:00
const token = uuid.v4(); // eslint-disable-line no-shadow
2018-06-02 15:50:39 +00:00
await ctx.redis.set(token, ctx.user.id, 'EX', 86400);
ctx.body = { token };
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
export async function transaction(ctx) {
const transactions = await models.transaction.findAll({
2016-08-07 14:38:05 +01:00
where: {
2018-06-02 15:50:39 +00:00
userId: ctx.user.id,
2016-08-07 14:38:05 +01:00
},
});
2015-07-09 23:01:43 +01:00
2018-06-02 18:07:00 +00:00
ctx.body = transactions.map(item => ({
id: item.id,
amount: item.amount / 100,
date: item.date,
description: item.description,
type: 'direct',
}));
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
export async function settings(ctx) {
2018-06-02 18:07:00 +00:00
ctx.assert(
ctx.request.body, 400,
'{"error": {"message": "Current Password required to update account.", "code": 612}}',
);
ctx.assert(
ctx.request.body.current_password, 400,
'{"error": {"message": "Current Password required to update account.", "code": 612}}',
);
2019-06-08 07:52:57 -07:00
const user = await models.user.findByPk(ctx.user.id);
2018-06-02 18:07:00 +00:00
ctx.assert(
await passwords.match(ctx.request.body.current_password, user.password), 400,
'{"error": {"message": "Incorrect password", "code": 606}}',
);
2018-06-02 15:50:39 +00:00
if (ctx.request.body.email && ctx.request.body.email !== user.email) {
user.email = ctx.request.body.email;
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
if (ctx.request.body.new_password) {
2018-06-02 18:07:00 +00:00
ctx.assert(
ctx.request.body.new_password.length >= 7, 400,
'{"error": {"message": "Password must be 7 or more characters long.", "code": 606}}',
);
2018-06-02 15:50:39 +00:00
user.password = await passwords.hash(ctx.request.body.new_password);
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
await user.save();
ctx.body = {};
2015-07-09 23:01:43 +01:00
}
2019-07-21 19:37:20 +00:00
export async function deleteUser(ctx) {
ctx.assert(
ctx.request.body, 400,
'{"error": {"message": "Current Password required to update account.", "code": 612}}',
);
ctx.assert(
ctx.request.body.current_password, 400,
'{"error": {"message": "Current Password required to update account.", "code": 612}}',
);
const user = await models.user.findByPk(ctx.user.id);
ctx.assert(
await passwords.match(ctx.request.body.current_password, user.password), 400,
'{"error": {"message": "Incorrect password", "code": 606}}',
);
await user.destroy();
ctx.body = '{"action":"logout", "message": "Account deleted"}';
}
2018-06-02 15:50:39 +00:00
export async function events(ctx) {
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) => {
2018-06-02 15:50:39 +00:00
ctx.websocket.send(message);
2015-08-08 20:37:49 +01:00
});
pubsub.on('ready', () => {
2018-06-02 15:50:39 +00:00
ctx.websocket.on('message', co.wrap(async (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');
2018-06-02 15:50:39 +00:00
ctx.websocket.send('Invalid authentication message. Bad JSON?');
2019-01-14 21:37:03 +00:00
ctx.Sentry.captureException(err);
2015-07-09 23:01:43 +01:00
}
2015-08-23 01:05:20 +01:00
try {
2018-06-02 15:50:39 +00:00
const reply = await ctx.redis.get(json.authorization);
2015-08-23 01:05:20 +01:00
if (reply) {
2016-06-06 15:37:00 +01:00
pubsub.subscribe(`/user/${reply}`);
2018-06-02 15:50:39 +00:00
ctx.websocket.send('{"status":"active"}');
2015-08-23 01:05:20 +01:00
debug('Subscribed to: /user/%s', reply);
} else {
2018-06-02 15:50:39 +00:00
ctx.websocket.send('Invalid authentication token.');
2015-08-23 01:05:20 +01:00
}
2016-06-06 15:37:00 +01:00
} catch (err) {
2015-08-23 01:05:20 +01:00
debug(err);
2019-01-14 21:37:03 +00:00
ctx.Sentry.captureException(err);
2015-07-09 23:01:43 +01:00
}
2018-06-02 15:50:39 +00:00
}));
2015-08-08 20:37:49 +01:00
});
2018-06-02 15:50:39 +00:00
ctx.websocket.on('close', () => {
2015-07-09 23:01:43 +01:00
debug('Socket closed');
pubsub.quit();
});
}