-
Notifications
You must be signed in to change notification settings - Fork 3
/
routes.js
59 lines (50 loc) · 1.36 KB
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var config = {
client_id: process.env.SLACK_CLIENT_ID,
client_secret: process.env.SLACK_CLIENT_SECRET,
};
var express = require('express');
var router = express.Router();
var request = require('request');
var AUTH_URL = 'https://slack.com/api/oauth.access';
router.get('/check', function(req, res, next) {
res.sendStatus(204);
});
router.post('/tokens', function(req, res, next) {
var code = req.body.authorizationCode;
if (!code) {
res.status(422);
res.json({ message: 'Missing parameter: authorizationCode' });
return;
}
var redirect_uri = req.body.redirectUri;
if (!redirect_uri) {
res.status(422);
res.json({ message: 'Missing parameter: redirectUri' });
return;
}
var params = {
client_id: config.client_id,
client_secret: config.client_secret,
code: code,
redirect_uri: redirect_uri
};
request.post({ url: AUTH_URL, form: params }, function(error, response, body) {
if (error || response.statusCode !== 200) {
res.status(500);
res.json({ message: 'Unknown error occurred' });
return;
}
data = JSON.parse(body);
if (!data.ok) {
res.status(400);
res.json({ message: body.error || 'Unknown error occurred' });
return;
}
res.status(201);
res.json({
accessToken: data.access_token,
scope: data.scope
});
});
});
module.exports = router;