-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify-refresh.js
35 lines (29 loc) · 1.03 KB
/
spotify-refresh.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
const request = require('request') // "Request" library
const errors = require('./errors').messageDictionary
const { SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET } = require("../vars")
module.exports = spotifyRefresh
function spotifyRefresh(req, res, next) {
if (req.jwt && req.jwt.spotify_refresh) {
const refresh_token = req.jwt.spotify_refresh
const config = {
url: 'https://accounts.spotify.com/api/token',
headers: { 'Authorization': 'Basic ' + (Buffer.from(SPOTIFY_CLIENT_ID + ':' + SPOTIFY_CLIENT_SECRET).toString('base64')) },
form: {
grant_type: 'refresh_token',
refresh_token: refresh_token
},
json: true
}
request.post(config, function(error, response, body) {
if (!error && response.statusCode === 200) {
const access_token = body.access_token;
req.jwt.spotify_access = access_token
next()
} else {
res.statusCode(401).send("Failed to refresh Spotify access token")
}
})
} else {
next(errors.invalidToken)
}
}