diff --git a/.eslintrc b/.eslintrc index 4b3b40eb..e539d312 100644 --- a/.eslintrc +++ b/.eslintrc @@ -13,5 +13,10 @@ "config": "webpack.config.js" } } - } + }, + "globals": { + __DEV__: true, + SLACK_KEY: true, + SLACK_REDIRECT_URI: true, + }, } diff --git a/frontend/components/SlackAuthLink/SlackAuthLink.js b/frontend/components/SlackAuthLink/SlackAuthLink.js index 0fa6aeba..c07735a9 100644 --- a/frontend/components/SlackAuthLink/SlackAuthLink.js +++ b/frontend/components/SlackAuthLink/SlackAuthLink.js @@ -16,22 +16,20 @@ class SlackAuthLink extends React.Component { 'identity.basic', 'identity.avatar', 'identity.team', - ] + ], } slackUrl = () => { const baseUrl = 'https://slack.com/oauth/authorize'; const params = { - client_id: '30086650419.30130733398', - scope: this.props.scopes.join(" "), - redirect_uri: __DEV__ ? - 'http://localhost:3000/auth/slack/' : - 'https://www.beautifulatlas.com/auth/slack/', + client_id: SLACK_KEY, + scope: this.props.scopes.join(' '), + redirect_uri: SLACK_REDIRECT_URI, state: this.props.user.getOauthState(), }; - const urlParams = Object.keys(params).map(function(key) { - return key + '=' + encodeURIComponent(params[key]); + const urlParams = Object.keys(params).map((key) => { + return `${key}=${encodeURIComponent(params[key])}`; }).join('&'); return `${baseUrl}?${urlParams}`; @@ -40,7 +38,7 @@ class SlackAuthLink extends React.Component { render() { return ( Authorize /w Slack - ) + ); } } diff --git a/frontend/scenes/SlackAuth/SlackAuth.js b/frontend/scenes/SlackAuth/SlackAuth.js index 4362e304..f4153380 100644 --- a/frontend/scenes/SlackAuth/SlackAuth.js +++ b/frontend/scenes/SlackAuth/SlackAuth.js @@ -5,6 +5,7 @@ import { observer } from 'mobx-react'; class SlackAuth extends React.Component { static propTypes = { user: React.PropTypes.object.isRequired, + location: React.PropTypes.object.isRequired, } componentDidMount = () => { @@ -19,4 +20,4 @@ class SlackAuth extends React.Component { } } -export default SlackAuth; \ No newline at end of file +export default SlackAuth; diff --git a/server/api/auth.js b/server/api/auth.js index 5ca77764..1a7dd9e9 100644 --- a/server/api/auth.js +++ b/server/api/auth.js @@ -1,7 +1,7 @@ import Router from 'koa-router'; import httpErrors from 'http-errors'; import fetch from 'isomorphic-fetch'; -var querystring = require('querystring'); +import querystring from 'querystring'; import { presentUser, presentTeam } from '../presenters'; import { User, Team } from '../models'; @@ -15,31 +15,29 @@ router.post('auth.slack', async (ctx) => { const body = { client_id: process.env.SLACK_KEY, client_secret: process.env.SLACK_SECRET, - code: code, redirect_uri: process.env.SLACK_REDIRECT_URI, - } + code, + }; let data; try { - const response = await fetch('https://slack.com/api/oauth.access?' + querystring.stringify(body)); + const response = await fetch(`https://slack.com/api/oauth.access?${querystring.stringify(body)}`); data = await response.json(); - } catch(e) { + } catch (e) { throw httpErrors.BadRequest(); } + console.log(data); + if (!data.ok) throw httpErrors.BadRequest(data.error); // Temp to block - let allowedSlackIds = process.env.ALLOWED_SLACK_IDS.split(','); - if (!allowedSlackIds.includes(data.team.id)) throw httpErrors.BadRequest("Invalid Slack team"); + const allowedSlackIds = process.env.ALLOWED_SLACK_IDS.split(','); + if (!allowedSlackIds.includes(data.team.id)) throw httpErrors.BadRequest('Invalid Slack team'); // User - let userData; let user = await User.findOne({ where: { slackId: data.user.id }}); - const authResponse = await fetch(`https://slack.com/api/auth.test?token=${data.access_token}`); - const authData = await authResponse.json(); - // Team let team = await Team.findOne({ where: { slackId: data.team.id } }); if (!team) { @@ -48,7 +46,7 @@ router.post('auth.slack', async (ctx) => { slackId: data.team.id, slackData: data.team, }); - const atlas = await team.createFirstAtlas(); + await team.createFirstAtlas(); } else { team.name = data.team.name; team.slackData = data.team; @@ -62,7 +60,7 @@ router.post('auth.slack', async (ctx) => { } else { user = await team.createUser({ slackId: data.user.id, - username: authData.user, + username: data.user.name, name: data.user.name, email: data.user.email, slackData: data.user, diff --git a/webpack.config.js b/webpack.config.js index 58e7db52..38d4b4cc 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -8,7 +8,8 @@ require('dotenv').config(); var definePlugin = new webpack.DefinePlugin({ __DEV__: JSON.stringify(JSON.parse(process.env.NODE_ENV !== 'production')), __PRERELEASE__: JSON.stringify(JSON.parse(process.env.BUILD_PRERELEASE || 'false')), - SLACK_REDIRECT_URI: process.env.SLACK_REDIRECT_URI, + SLACK_REDIRECT_URI: JSON.stringify(process.env.SLACK_REDIRECT_URI), + SLACK_KEY: JSON.stringify(process.env.SLACK_KEY), }); module.exports = {