-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
197 lines (150 loc) · 4.42 KB
/
server.js
File metadata and controls
197 lines (150 loc) · 4.42 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var HTTP_PORT = process.env.PORT || 8080
var express = require("express");
var auth = require("./accountSystem.js");
var inventorySystem = require("./inventorySystem.js");
var database = require("./database.js");
var path = require("path");
var fs = require("fs");
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const clientSessions = require('client-sessions');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
var inventory;
/*
Sessions and logins
*/
app.use(clientSessions({
cookieName: "session", // this is the object name that will be added to 'req'
secret: "randomlongstring", // this should be a long un-guessable string.
duration: 60 * 60 * 1000, // milliseconds
activeDuration: 60000 * 60 // the session will be extended by this many ms each request (1 minute)
}));
app.post("/login", (req, res) => {
auth.checkPassword(req.body.username, req.body.password).then((data) => {
req.session.user = {
username: data.username,
powerLevel: data.powerLevel
}
//redirect somewhere
res.send("Welcome " + req.session.user.username);
}).catch(() => {
//redirect back to login page with error
// res.status(403).send("403");
res.send("incorrect password");
});
});
app.get('/logout', function (req, res, next) {
req.session.reset();
res.redirect('/');
});
function checkLogin(accessLevel) {
return function loginFunction(req, res, next) {
if (!req.session.user) {
res.redirect("/login");
} else {
if (req.session.user.powerLevel < accessLevel) {
//redirect to 403 forbidden
}
else
next();
}
}
}
function checkLoginSpecific(accessLevel) {
return function loginFunction(req, res, next) {
if (!req.session.user) {
res.redirect("/login");
} else {
if (req.session.user.powerLevel != accessLevel || req.session.user.powerLevel != 100) {
//redirect to 403 forbidden
}
else
next();
}
}
}
/*
Accounts
*/
//user register themselves
app.post("/register", (req, res) => {
let data = {
username: req.body.username,
password: req.body.password,
email: req.body.email,
name: req.body.name
};
auth.createAccount(data).then(() => {
res.send('Account created for' + data.name);
}).catch((err) => {
res.send(err);
});
//check if containing empty fields
});
//manually creating an account
app.post("/createAccount", checkLogin(100), (req, res) => {
let data = req.body;
auth.createAccount(data).then(() => {
res.send('Account created for' + data.name);
});
//check if containing empty fields
});
//update account
app.post("/updateAccount", checkLogin(100), (req, res) => {
let data = req.body;
});
//delete account
app.delete("/deleteAccount", checkLogin(100), (req, res) => {
let data = req.body;
});
/*
Checkouts
*/
app.post("/requestCheckout", checkLogin(1), (req, res) => {
let data = req.body;
});
//user requesting checkout
app.post("/checkout", checkLogin(1), (req, res) => {
let data = req.body;
});
app.post("/updateCheckout", checkLogin(5), (req, res) => {
let data = req.body;
});
app.get("/user/:username", checkLogin(1), (req, res) => {
auth.getUserByUsername(req.params.username).then((data) => {
res.send(data);
})
});
/*
* Test Code
*/
app.get("/login", (req, res) => {
res.sendFile(path.join(__dirname + "/test pages/login.html"))
});
app.get("/forbidden", checkLogin(100), (req, res) => {
res.send("hi");
});
app.get("/createAccount", checkLogin(100), (req, res) => {
res.sendFile(path.join(__dirname + "/test pages/createAccount.html"))
});
app.get("/", (req, res) => {
res.send("Home Page");
});
app.get("/register", (req, res) => {
res.sendFile(path.join(__dirname + "/test pages/register.html"))
});
app.use((req, res) => {
res.status(404).send("Page Not Found");
});
/**
* Starting the server
*/
database.initialize().then(() => {
auth.setDatabase(database.getDatabase());
inventory = inventorySystem(database.getDatabase());
console.log("Server now listening to:" + HTTP_PORT);
app.listen(HTTP_PORT);
}).catch((err) => {
console.log("Database failed to start:" + err);
});