Fixed auth

This commit is contained in:
Jori Lallo
2016-07-25 22:49:32 -07:00
parent c1c8644578
commit f08087ebd1
5 changed files with 28 additions and 25 deletions

View File

@ -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,