Update stuff
This commit is contained in:
parent
0254e42b9c
commit
553ba9db9a
40 changed files with 7343 additions and 717 deletions
|
@ -9,24 +9,24 @@ const debug = debugname('hostr-api:user');
|
|||
|
||||
const redisUrl = process.env.REDIS_URL;
|
||||
|
||||
export function* get() {
|
||||
this.body = this.user;
|
||||
export async function get(ctx) {
|
||||
ctx.body = ctx.user;
|
||||
}
|
||||
|
||||
export function* token() {
|
||||
export async function token(ctx) {
|
||||
const token = uuid.v4(); // eslint-disable-line no-shadow
|
||||
yield this.redis.set(token, this.user.id, 'EX', 86400);
|
||||
this.body = { token };
|
||||
await ctx.redis.set(token, ctx.user.id, 'EX', 86400);
|
||||
ctx.body = { token };
|
||||
}
|
||||
|
||||
export function* transaction() {
|
||||
const transactions = yield models.transaction.findAll({
|
||||
export async function transaction(ctx) {
|
||||
const transactions = await models.transaction.findAll({
|
||||
where: {
|
||||
userId: this.user.id,
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
});
|
||||
|
||||
this.body = transactions.map((item) => {
|
||||
ctx.body = transactions.map((item) => {
|
||||
return {
|
||||
id: item.id,
|
||||
amount: item.amount / 100,
|
||||
|
@ -37,57 +37,57 @@ export function* transaction() {
|
|||
});
|
||||
}
|
||||
|
||||
export function* settings() {
|
||||
this.assert(this.request.body, 400,
|
||||
export async function settings(ctx) {
|
||||
ctx.assert(ctx.request.body, 400,
|
||||
'{"error": {"message": "Current Password required to update account.", "code": 612}}');
|
||||
this.assert(this.request.body.current_password, 400,
|
||||
ctx.assert(ctx.request.body.current_password, 400,
|
||||
'{"error": {"message": "Current Password required to update account.", "code": 612}}');
|
||||
const user = yield models.user.findById(this.user.id);
|
||||
this.assert(yield passwords.match(this.request.body.current_password, user.password), 400,
|
||||
const user = await models.user.findById(ctx.user.id);
|
||||
ctx.assert(await passwords.match(ctx.request.body.current_password, user.password), 400,
|
||||
'{"error": {"message": "Incorrect password", "code": 606}}');
|
||||
if (this.request.body.email && this.request.body.email !== user.email) {
|
||||
user.email = this.request.body.email;
|
||||
if (ctx.request.body.email && ctx.request.body.email !== user.email) {
|
||||
user.email = ctx.request.body.email;
|
||||
}
|
||||
if (this.request.body.new_password) {
|
||||
this.assert(this.request.body.new_password.length >= 7, 400,
|
||||
if (ctx.request.body.new_password) {
|
||||
ctx.assert(ctx.request.body.new_password.length >= 7, 400,
|
||||
'{"error": {"message": "Password must be 7 or more characters long.", "code": 606}}');
|
||||
user.password = yield passwords.hash(this.request.body.new_password);
|
||||
user.password = await passwords.hash(ctx.request.body.new_password);
|
||||
}
|
||||
yield user.save();
|
||||
this.body = {};
|
||||
await user.save();
|
||||
ctx.body = {};
|
||||
}
|
||||
|
||||
export function* events() {
|
||||
export async function events(ctx) {
|
||||
const pubsub = redis.createClient(redisUrl);
|
||||
pubsub.on('message', (channel, message) => {
|
||||
this.websocket.send(message);
|
||||
ctx.websocket.send(message);
|
||||
});
|
||||
pubsub.on('ready', () => {
|
||||
this.websocket.on('message', co.wrap(function* wsMessage(message) {
|
||||
ctx.websocket.on('message', co.wrap(async (message) => {
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(message);
|
||||
} catch (err) {
|
||||
debug('Invalid JSON for socket auth');
|
||||
this.websocket.send('Invalid authentication message. Bad JSON?');
|
||||
this.raven.captureError(err);
|
||||
ctx.websocket.send('Invalid authentication message. Bad JSON?');
|
||||
ctx.raven.captureError(err);
|
||||
}
|
||||
try {
|
||||
const reply = yield this.redis.get(json.authorization);
|
||||
const reply = await ctx.redis.get(json.authorization);
|
||||
if (reply) {
|
||||
pubsub.subscribe(`/user/${reply}`);
|
||||
this.websocket.send('{"status":"active"}');
|
||||
ctx.websocket.send('{"status":"active"}');
|
||||
debug('Subscribed to: /user/%s', reply);
|
||||
} else {
|
||||
this.websocket.send('Invalid authentication token.');
|
||||
ctx.websocket.send('Invalid authentication token.');
|
||||
}
|
||||
} catch (err) {
|
||||
debug(err);
|
||||
this.raven.captureError(err);
|
||||
ctx.raven.captureError(err);
|
||||
}
|
||||
}.bind(this)));
|
||||
}));
|
||||
});
|
||||
this.websocket.on('close', () => {
|
||||
ctx.websocket.on('close', () => {
|
||||
debug('Socket closed');
|
||||
pubsub.quit();
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue