Request time cache, tracking collaborators etc

This commit is contained in:
Jori Lallo
2016-08-15 12:51:26 +02:00
parent 94e39d74bf
commit 11f6c533b8
15 changed files with 141 additions and 33 deletions

View File

@@ -72,10 +72,10 @@ router.post('auth.slack', async (ctx) => {
}
ctx.body = { data: {
user: await presentUser(user),
team: await presentTeam(team),
user: await presentUser(ctx, user),
team: await presentTeam(ctx, team),
accessToken: user.getJwtToken(),
}};
} };
});
export default router;

View File

@@ -10,7 +10,7 @@ export default function auth({ require = true } = {}) {
const authorizationHeader = ctx.request.get('authorization');
if (authorizationHeader) {
const parts = authorizationHeader.split(' ');
if (parts.length == 2) {
if (parts.length === 2) {
const scheme = parts[0];
const credentials = parts[1];
@@ -35,7 +35,7 @@ export default function auth({ require = true } = {}) {
let payload;
try {
payload = JWT.decode(token);
} catch(_e) {
} catch (e) {
throw httpErrors.Unauthorized('Unable to decode JWT token');
}
const user = await User.findOne({
@@ -44,19 +44,20 @@ export default function auth({ require = true } = {}) {
try {
JWT.verify(token, user.jwtSecret);
} catch(e) {
} catch (e) {
throw httpErrors.Unauthorized('Invalid token');
}
ctx.state.token = token;
ctx.state.user = user;
ctx.cache[user.id] = user;
}
return next();
};
};
}
// Export JWT methods as a convenience
export const sign = JWT.sign;
export const sign = JWT.sign;
export const verify = JWT.verify;
export const decode = JWT.decode;

View File

@@ -28,7 +28,7 @@ router.post('collections.create', auth(), async (ctx) => {
});
ctx.body = {
data: await presentCollection(atlas, true),
data: await presentCollection(ctx, atlas, true),
};
});
@@ -47,7 +47,7 @@ router.post('collections.info', auth(), async (ctx) => {
if (!atlas) throw httpErrors.NotFound();
ctx.body = {
data: await presentCollection(atlas, true),
data: await presentCollection(ctx, atlas, true),
};
});
@@ -68,7 +68,7 @@ router.post('collections.list', auth(), pagination(), async (ctx) => {
// Atlases
let data = [];
await Promise.all(collections.map(async (atlas) => {
return data.push(await presentCollection(atlas, true));
return data.push(await presentCollection(ctx, atlas, true));
}));
data = _orderBy(data, ['updatedAt'], ['desc']);
@@ -96,7 +96,7 @@ router.post('collections.updateNavigationTree', auth(), async (ctx) => {
await atlas.updateNavigationTree(tree);
ctx.body = {
data: await presentCollection(atlas, true),
data: await presentCollection(ctx, atlas, true),
};
});

View File

@@ -34,11 +34,11 @@ router.post('documents.info', auth({ require: false }), async (ctx) => {
}
ctx.body = {
data: await presentDocument(document, true),
data: await presentDocument(ctx, document, true),
};
} else {
ctx.body = {
data: await presentDocument(document),
data: await presentDocument(ctx, document),
};
}
});
@@ -118,6 +118,7 @@ router.post('documents.create', auth(), async (ctx) => {
teamId: user.teamId,
userId: user.id,
lastModifiedById: user.id,
createdById: user.id,
title,
text,
});
@@ -166,7 +167,7 @@ router.post('documents.update', auth(), async (ctx) => {
}
ctx.body = {
data: await presentDocument(document, true),
data: await presentDocument(ctx, document, true),
};
});

View File

@@ -11,6 +11,7 @@ import documents from './documents';
import validation from './validation';
import methodOverride from '../middlewares/methodOverride';
import cache from '../middlewares/cache';
const api = new Koa();
const router = new Router();
@@ -42,6 +43,7 @@ api.use(async (ctx, next) => {
api.use(bodyParser());
api.use(methodOverride());
api.use(cache());
api.use(validation());
router.use('/', auth.routes());

View File

@@ -11,7 +11,7 @@ import { presentUser } from '../presenters';
const router = new Router();
router.post('user.info', auth(), async (ctx) => {
ctx.body = { data: await presentUser(ctx.state.user) };
ctx.body = { data: await presentUser(ctx, ctx.state.user) };
});
router.post('user.s3Upload', auth(), async (ctx) => {