-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauthd.js
44 lines (40 loc) · 998 Bytes
/
oauthd.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
//
// This is a library file that handles the OAuth flow with Slack.
//
var rq = require('request');
module.exports = {
oauth: oauth
};
// https://api.slack.com/methods/oauth.access
function oauth(code, uri) {
return post({
url: 'https://slack.com/api/oauth.access',
transform: JSON.parse,
form: {
client_id: process.env.SLACK_CLIENT_ID,
client_secret: process.env.SLACK_CLIENT_SECRET,
code: code
}
});
}
// teensy conversion to Promise-based API
function post(options) {
return new Promise(function(resolve, reject) {
rq.post(options, function(error, response, body) {
if (error) {
console.log('http error');
reject(error);
}
if (options.transform) {
body = options.transform(body);
}
if (body.ok) {
console.log('success');
resolve(body);
} else {
console.log('API error');
reject(body.error || body);
}
});
});
}