-
Notifications
You must be signed in to change notification settings - Fork 0
/
session-controller.js
66 lines (50 loc) · 1.46 KB
/
session-controller.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const {v4 : uuidv4 } = require('uuid');
const {SESSION_KEY } = require('./config');
class SessionController {
constructor(knex_object){
this.sessions_list = [];
this.lifespan = 24*60*60*1000;
this.genID = ()=> uuidv4(SESSION_KEY);
this.knex = knex_object;
let scheduler = async () => {
await this.updateList();
setTimeout(
scheduler
, 5000);
}
scheduler();
}
async setSession(){
let session_id = this.genID();
let epoch_time = new Date().getTime()
await this.knex.insert( {
id: session_id,
epoch_time,
})
.into('sessions');
return session_id;
}
async updateList(){
//
try {
let sessions = await this.knex.select().from('sessions');
console.log("sessions :");
console.log(sessions);
this.sessions_list = sessions;
this.deleteExpired();
} catch (error) {
console.log(error);
}
}
async deleteExpired(){
let epoch_time = new Date().getTime() - this.lifespan ;
await this.knex.delete().from('sessions').where('epoch_time','<', epoch_time );
}
async deleteSession(sid){
//
await this.knex.delete().from('sessions').where('id', sid );
}
}
module.exports = {
SessionController
}