-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.js
More file actions
45 lines (37 loc) · 1.08 KB
/
Copy pathsession.js
File metadata and controls
45 lines (37 loc) · 1.08 KB
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
const client = require('./client');
class Session {
constructor(slpSession, email, password) {
const {
expirationDate,
userId,
token
} = slpSession;
this.expirationDate = new Date(expirationDate);
this.userId = userId;
this.token = token;
this.email = email;
this.password = password;
}
static async create(email, password) {
const res = await client.login(email, password);
return new Session(res.session, email, password);
}
isValid() {
return this.expirationDate.getTime() - Date.now() > 0;
}
async refreshToken() {
const res = await client.login(this.email, this.password);
const {
expirationDate,
userId,
token
} = res.session;
if (!expirationDate || !token) {
throw new Error('Invalid 8SLP session data');
}
this.expirationDate = new Date(expirationDate);
this.userId = userId;
this.token = token;
}
}
module.exports = Session;