-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken.js
49 lines (43 loc) · 1.29 KB
/
token.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
const rp = require('request-promise')
const config = require('./lib/config')
const netlifyUrl = require('./lib/netlifyUrl')
const redirect = require('./lib/redirect')
const {
clientId,
devClientId,
clientSecret,
devClientSecret,
apiPath,
appPath
} = config
exports.handler = async (event, context) => {
const { error, code } = event.queryStringParameters
const { siteUrl, production } = netlifyUrl(event, context)
if (error) {
return { statusCode: 403, body: 'Error' }
}
const access = await rp({
uri: 'https://slack.com/api/oauth.access',
json: true,
qs: {
code,
client_id: production ? clientId : devClientId,
client_secret: production ? clientSecret : devClientSecret,
redirect_uri: `${siteUrl}${apiPath}/token`
}
})
if (access.error || !access.ok) {
return {
statusCode: 403,
body: access.error || 'Not ok'
}
}
return redirect(
`${siteUrl}${appPath}${
// This makes it easier to test that we are redirecting with the proper
// information since we cant access the url hash from the request. This
// is a good thing as far as the app is concerned, but makes it impossible to test/
process.env.NODE_ENV === 'test' ? '?query=' : '#'
}${encodeURIComponent(JSON.stringify(access))}`
)
}