-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacadeIN.js
112 lines (90 loc) · 3.25 KB
/
facadeIN.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
const {webSettings} = require("./setting");
const views = require("./factories/viewsFolder");
const {reportMethod} = require("./methods/reportMethod");
const UserBuilder = require("./factories/userBuilder");
const login = async (sMethod)=>{
const method = sMethod.method;
const uBuilder = new UserBuilder();
var response = null;
// -----------starts here
if (method.type=="GET"){
response = await views.render(method.url.pathname);
}else if (method.type=="POST"){
var user,token;
({user,token} = await uBuilder.create(method));
if(user.err){
method.setToken(token,false,5000);
redirect(method,'/login')
}
else{
method.setToken(token,true,50000000);
await sMethod.addUser(user);
redirect(method,user.mainPage);
}
}
// -----------------ends here
return(response);
}
const tokenedAccess = async (sMethod)=>{
var response = null;
const method = sMethod.method;
// ---------------- starts here
const token = method.getToken();
if(token){
const user = sMethod.getUser(token);
if(user){
user.setLastUsedTime(new Date().getTime());
if(method.getPath(1) == 'api'){
//api method
const apiMethod = method.getApiMethod();
await apiMethod.setQuery();
response = await apiMethod.execute(user.apiAccessControl(method.getPath(2),method.type));
}
else if(method.getPath(1) == 'report'){
const report = new reportMethod(method);
response = await report.execute(user.type);
}
else if(method.getPath(1) == 'signup' && user.apiAccessControl(method.getPath(1),method.type)){
response = await sMethod.signup();
}
else if(method.getPath(1) == 'changePass'&& user.apiAccessControl(method.getPath(1),method.type)){
response = await sMethod.changePass();
}
else if(method.getPath(1) == 'logOut'){
await sMethod.logOut(token);
redirect(method,'/login');
}
else{
//render views
if(method.getPath(1) == ''){
redirect(method,user.mainPage);
}
else if (user.viewAccessControl(method.url.pathname)){
response = await views.render(method.url.pathname);
}
else if(user.viewAccessControl(method.url.pathname) == false){
redirect(method,'/notAllowed');
}
else{
redirect(method,'/pageNotFound');
}
}
}
else{
redirect(method,'/login');
}
}
else{
redirect(method,'/login');
}
// -------------------- ends here
return(response);
}
// ------------------------------------------------------------------------------------
function redirect(method,path){
method.res.writeHead(302,{'Content-Type':'text/plain','Location':webSettings.protocol+"://"+webSettings.host+path});
}
module.exports = {
login,
tokenedAccess
};