Unfurling of Slack links (#487)

* First pass: Unfurling of Slack links

* Add authentication in db

* Call associate on Event correctly

* Add SLACK_APP_ID, remove SLACK_REDIRECT_URI, tidy env sample

* PR feedback

* Comment clarify
This commit is contained in:
Tom Moor
2017-12-18 19:59:29 -08:00
committed by GitHub
parent 938bb3fc31
commit 32ba98bb1a
19 changed files with 219 additions and 48 deletions

View File

@ -2,7 +2,7 @@
import Router from 'koa-router';
import auth from './middlewares/authentication';
import { presentUser, presentTeam } from '../presenters';
import { User, Team } from '../models';
import { Authentication, User, Team } from '../models';
import * as Slack from '../slack';
const router = new Router();
@ -81,11 +81,21 @@ router.post('auth.slack', async ctx => {
};
});
router.post('auth.slackCommands', async ctx => {
router.post('auth.slackCommands', auth(), async ctx => {
const { code } = ctx.body;
ctx.assertPresent(code, 'code is required');
await Slack.oauthAccess(code, `${process.env.URL || ''}/auth/slack/commands`);
const user = ctx.state.user;
const endpoint = `${process.env.URL || ''}/auth/slack/commands`;
const data = await Slack.oauthAccess(code, endpoint);
await Authentication.create({
serviceId: 'slack',
userId: user.id,
teamId: user.teamId,
token: data.access_token,
scopes: data.scope.split(','),
});
});
export default router;