diff --git a/app/scenes/Settings/Slack.js b/app/scenes/Settings/Slack.js index 9f6bfc890..82e1ee0aa 100644 --- a/app/scenes/Settings/Slack.js +++ b/app/scenes/Settings/Slack.js @@ -11,12 +11,14 @@ import HelpText from 'components/HelpText'; import SlackButton from './components/SlackButton'; import CollectionsStore from 'stores/CollectionsStore'; import IntegrationsStore from 'stores/IntegrationsStore'; +import AuthStore from 'stores/AuthStore'; import Notice from 'shared/components/Notice'; import getQueryVariable from 'shared/utils/getQueryVariable'; type Props = { collections: CollectionsStore, integrations: IntegrationsStore, + auth: AuthStore, }; @observer @@ -36,7 +38,8 @@ class Slack extends React.Component { } render() { - const { collections, integrations } = this.props; + const { collections, integrations, auth } = this.props; + const teamId = auth.team ? auth.team.id : ''; return ( @@ -48,6 +51,12 @@ class Slack extends React.Component { Outline to your team. Try again? )} + {this.error === 'unauthenticated' && ( + + Something went wrong while authenticating your request. Please try + logging in again? + + )} Preview Outline links your team mates share and use the{' '} /outline slash command in Slack to search for documents @@ -60,7 +69,7 @@ class Slack extends React.Component { )}

@@ -130,4 +139,4 @@ const Code = styled.code` border-radius: 4px; `; -export default inject('collections', 'integrations')(Slack); +export default inject('collections', 'integrations', 'auth')(Slack); diff --git a/server/auth/slack.js b/server/auth/slack.js index c013a91d1..89568bc18 100644 --- a/server/auth/slack.js +++ b/server/auth/slack.js @@ -69,8 +69,9 @@ router.get('slack.callback', auth({ required: false }), async ctx => { ctx.signIn(user, team, 'slack', isFirstSignin); }); -router.get('slack.commands', auth(), async ctx => { - const { code, error } = ctx.request.query; +router.get('slack.commands', auth({ required: false }), async ctx => { + const { code, state, error } = ctx.request.query; + const user = ctx.state.user; ctx.assertPresent(code || error, 'code is required'); if (error) { @@ -78,9 +79,28 @@ router.get('slack.commands', auth(), async ctx => { return; } + // this code block accounts for the root domain being unable to + // access authentcation for subdomains. We must forward to the appropriate + // subdomain to complete the oauth flow + if (!user) { + if (state) { + try { + const team = await Team.findById(state); + return ctx.redirect( + `${team.url}/auth${ctx.request.path}?${ctx.request.querystring}` + ); + } catch (err) { + return ctx.redirect( + `/settings/integrations/slack?error=unauthenticated` + ); + } + } else { + return ctx.redirect(`/settings/integrations/slack?error=unauthenticated`); + } + } + const endpoint = `${process.env.URL || ''}/auth/slack.commands`; const data = await Slack.oauthAccess(code, endpoint); - const user = ctx.state.user; const authentication = await Authentication.create({ service: 'slack',