-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.js
125 lines (96 loc) · 2.65 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
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
const log = false;
let token,user,user_type,uid,session_type;
function check(){
let type = engine.data.get('session_type','local');
if(!type){
return false;
}
if(type == 'temp'){
token = engine.data.get('token','session');
user = engine.data.get('user','session');
user_type = engine.data.get('user_type','session');
uid = engine.data.get('uid','session');
session_type = engine.data.get('session_type','local');
} else {
token = engine.data.get('token','local');
user = engine.data.get('user','local');
user_type = engine.data.get('user_type','local');
uid = engine.data.get('uid','local');
session_type = engine.data.get('session_type','local');
}
if(!token){
return false;
}
return true;
}
function end(){
let type = engine.data.get('session_type','local');
if(!type){
return false;
}
if(type == 'temp'){
token = engine.data.delete('token','session');
user = engine.data.delete('user','session');
user_type = engine.data.delete('user_type','session');
uid = engine.data.delete('uid','session');
session_type = engine.data.delete('session_type','local');
} else {
token = engine.data.delete('token','local');
user = engine.data.delete('user','local');
user_type = engine.data.delete('user_type','local');
uid = engine.data.delete('uid','local');
session_type = engine.data.delete('session_type','local');
}
return true;
}
module.exports = {
check : check,
start : function(token_arg,user_arg,uid,remember){
engine.common.tell('starting-session',log);
if(!token_arg){
return engine.common.error("not_found-token");
}
if(remember === true){
engine.data.reset('token',token_arg,'local');
engine.data.reset('user',user_arg,'local');
engine.data.reset('uid',uid,'local');
engine.data.reset('session_type','persistant','local');
} else {
engine.data.reset('token',token_arg,'session');
engine.data.reset('user',user_arg,'session');
engine.data.reset('uid',uid,'session');
engine.data.reset('session_type','temp','local');
}
return check();
},
end : end,
token : token,
uid : uid,
user : user,
user_type : user_type,
session_type:session_type,
get : {
user: function(){
if(check() == false){
return null;
}
if(user_type == "object"){
return JSON.parse(user);
} else {
return user;
}
},
token:function(){
check();
return token;
},
uid:function(){
check();
return uid;
},
session_type:function(){
check();
return session_type;
},
}
};