-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.js
50 lines (39 loc) · 1.13 KB
/
session.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
50
const constants = require('./constants');
const got = require('got');
// stores cookie -> Session objects
const sessions = {};
class Session {
constructor(cookie, access_token = null, refresh_token = null) {
this.access_token = access_token;
this.refresh_token = refresh_token;
if (sessions[cookie]) {
throw Error("Duplicate session cookie!");
}
sessions[cookie] = this;
}
async init() {
const { body } = await got(constants.BASE_URL + "/me/", {
headers: { 'Authorization': 'Bearer ' + this.access_token },
responseType: 'json'
});
this.name = body.display_name;
this.image = body.images[0].url;
}
async refreshAccessToken() {
const authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
grant_type: 'refresh_token',
refresh_token: this.refresh_token
},
headers: constants.CLIENT_AUTH_HEADER,
responseType: 'json'
};
const { body } = await got.post(authOptions);
this.access_token = body.access_token;
}
};
module.exports = {
sessions,
Session
};