-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
36 lines (31 loc) · 845 Bytes
/
auth.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
require('dotenv').config()
const request = require("request")
const cache = require("./services/cache")
function getAuthentication() {
var options = {
uri: "https://accounts.spotify.com/api/token",
method: 'POST',
headers: {
"Authorization": "Basic " + (Buffer.from(process.env.SPOTIFY_ID + ":" + process.env.SPOTIFY_SECRET).toString('base64'))
},
form: {
"grant_type": "client_credentials"
},
json: true
};
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if(error){
return reject("Fail to retrieve acess token: " + error);
}
if (body.access_token) {
cache.setUserToken(body.access_token)
resolve();
}
else {
reject("Invalid Token")
}
});
});
}
getAuthentication()