-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.js
44 lines (39 loc) · 1.12 KB
/
tools.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
const axios = require('axios')
const usersDb = require('./users/db')
const config = require('./config')
exports.refresh = async (_id) => {
const { refresh_token } = await usersDb.findOneAsync({ _id })
try {
const res = await axios.post(
'https://accounts.spotify.com/api/token',
{
grant_type: 'refresh_token',
refresh_token,
},
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${Buffer.from(
`${config.clientId}:${config.clientSecret}`,
).toString('base64')}`,
},
},
)
if (!res.data || !res.data.access_token) {
throw new Error(`No data (${res.data}) <${res.statusCode}>`)
}
// eslint-disable-next-line camelcase
const { access_token } = res.data
await usersDb.updateAsync({ _id }, { $set: { access_token: access_token } })
return access_token
} catch (e) {
if (e.response) {
throw new Error(
e.response.data.error_description ||
e.response.data.error ||
JSON.stringify(e.response.data),
)
}
throw e
}
}