-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionHandler.js
More file actions
152 lines (119 loc) · 3.99 KB
/
sessionHandler.js
File metadata and controls
152 lines (119 loc) · 3.99 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Created by arun on 15/6/17.
*/
// DEPENDENCY
// ioredis
// addsession
// It works as a normail callBack function , the user id is passed as first parameter and a callback function as next parameter,
// the call callBack will be called when the session is created and which will be having the session token
// validate session
//Tokens are retrived from the request using the token key (req.body.token) and the
// response result is passed in the request itself as sessionValue (req.body.sessionValue )
// removeSession
// It also takes token from request as (req.body.token)
const redis = require('ioredis');
let config = require('../../config');
var stringUtils = require('./stringOperations');
module.exports = self = {
addSession (id, callBack =()=>{}){
let token = stringUtils.generate_key();
let client = new redis({
port: config.redisPort,
host: config.redisUrl
});
client.set(token, id, (error, data) => {
if (error) {
console.log("set error redis",error)
callBack(false);
} else {
client.expire(token, config.sessionTimeout, (setExpError, setExpData) => {
client.disconnect();
if (setExpError) { console.log("session error",setExpError)
callBack(false)
}
else {
callBack(token);
}
});
}
})
},
validateSession(req, res, next){
var client = new redis({
port: config.redisPort,
host: config.redisUrl
});
var token = req.body.token;
if(!token){
token = req.query.token;
}
console.log(token,req.body,'token check');
client.get(token, (error, data) => {
if (error) {
res.send({
success: false,
error: error
})
}
else {
console.log(data,'token data')
if (data !== null) {
client.expire(token, config.sessionTimeout, (setExpError, setExpData) => {
client.disconnect();
if (setExpError) {
res.send({
success: false,
error: error
})
}
else {
req.body.sessionValue = data;
next();
}
});
}
else {
if(req.method === 'GET'){
res.send(`
<html>
<body style="text-align: center">
<h>Session Expired Please Login</h>
</body>
</html>
`);
return;
}
res.status(config.errorCodeUnauthorised);
res.send({
success: false,
error: config.InvalidSession,
status:config.errorCodeUnauthorised
})
}
}
})
},
removeSession(req, res, next){
console.log(req.body)
var client = new redis({
port: config.redisPort,
host: config.redisUrl
});
var token = req.body.token;
if(!token){
token = req.query.token;
}
client.del(token, (error, data) => {
client.disconnect();
if (error) {
res.send({
success: false,
error: error
})
}
else {
next();
}
})
}
};