This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/api/apiKeys.js
2016-08-26 22:04:28 -07:00

77 lines
1.5 KiB
JavaScript

import Router from 'koa-router';
import httpErrors from 'http-errors';
import _ from 'lodash';
import auth from './authentication';
import pagination from './middlewares/pagination';
import { presentApiKey } from '../presenters';
import { ApiKey } from '../models';
const router = new Router();
router.post('apiKeys.create', auth(), async (ctx) => {
const {
name,
} = ctx.body;
ctx.assertPresent(name, 'name is required');
const user = ctx.state.user;
const key = await ApiKey.create({
name,
userId: user.id,
});
ctx.body = {
data: presentApiKey(ctx, key),
};
});
router.post('apiKeys.list', auth(), pagination(), async (ctx) => {
const user = ctx.state.user;
const keys = await ApiKey.findAll({
where: {
userId: user.id,
},
order: [
['createdAt', 'DESC'],
],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
const data = keys.map(key => {
return presentApiKey(ctx, key);
});
ctx.body = {
pagination: ctx.state.pagination,
data,
};
});
router.post('apiKeys.delete', auth(), async (ctx) => {
const {
id,
} = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const key = await ApiKey.findById(id);
if (!key || key.userId !== user.id) throw httpErrors.BadRequest();
// Delete the actual document
try {
await key.destroy();
} catch (e) {
throw httpErrors.BadRequest('Error while deleting key');
}
ctx.body = {
success: true,
};
});
export default router;