From 662f908c7662ce671db0074f6d2e667de363fd77 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 2 Aug 2018 23:26:44 -0700 Subject: [PATCH] Allow Slack integration setup for Google Auth accounts --- server/auth/slack.js | 20 +++++--------------- server/middlewares/authentication.js | 6 ++++-- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/server/auth/slack.js b/server/auth/slack.js index 445316839..ecfa562ef 100644 --- a/server/auth/slack.js +++ b/server/auth/slack.js @@ -1,5 +1,6 @@ // @flow import Router from 'koa-router'; +import auth from '../middlewares/authentication'; import addHours from 'date-fns/add_hours'; import addMonths from 'date-fns/add_months'; import { slackAuth } from '../../shared/utils/routeHelpers'; @@ -77,7 +78,7 @@ router.get('slack.callback', async ctx => { ctx.redirect('/'); }); -router.get('slack.commands', async ctx => { +router.get('slack.commands', auth(), async ctx => { const { code, error } = ctx.request.query; ctx.assertPresent(code || error, 'code is required'); @@ -88,12 +89,7 @@ router.get('slack.commands', async ctx => { const endpoint = `${process.env.URL || ''}/auth/slack.commands`; const data = await Slack.oauthAccess(code, endpoint); - const user = await User.find({ - where: { - service: 'slack', - serviceId: data.user_id, - }, - }); + const user = ctx.state.user; const authentication = await Authentication.create({ service: 'slack', @@ -114,7 +110,7 @@ router.get('slack.commands', async ctx => { ctx.redirect('/settings/integrations/slack'); }); -router.get('slack.post', async ctx => { +router.get('slack.post', auth(), async ctx => { const { code, error, state } = ctx.request.query; ctx.assertPresent(code || error, 'code is required'); @@ -128,13 +124,7 @@ router.get('slack.post', async ctx => { const endpoint = `${process.env.URL || ''}/auth/slack.post`; const data = await Slack.oauthAccess(code, endpoint); - - const user = await User.find({ - where: { - service: 'slack', - serviceId: data.user_id, - }, - }); + const user = ctx.state.user; const authentication = await Authentication.create({ service: 'slack', diff --git a/server/middlewares/authentication.js b/server/middlewares/authentication.js index 59a0e5771..67f570ef1 100644 --- a/server/middlewares/authentication.js +++ b/server/middlewares/authentication.js @@ -24,10 +24,12 @@ export default function auth(options?: { required?: boolean } = {}) { ); } // $FlowFixMe - } else if (ctx.body.token) { + } else if (ctx.body && ctx.body.token) { token = ctx.body.token; } else if (ctx.request.query.token) { token = ctx.request.query.token; + } else if (ctx.cookies.get('accessToken')) { + token = ctx.cookies.get('accessToken'); } if (!token && options.required !== false) { @@ -84,7 +86,7 @@ export default function auth(options?: { required?: boolean } = {}) { ctx.state.token = token; ctx.state.user = user; - // $FlowFixMe + if (!ctx.cache) ctx.cache = {}; ctx.cache[user.id] = user; }