-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
320 lines (287 loc) · 7.8 KB
/
server.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* Chess studies backend v0.26
* v0.26 : 2021-11-11
* Philippe Marc Meyer 2021
*/
'use strict';
const express = require('express');
const cors = require('cors'); //import cors module
const bcrypt = require('bcrypt');
const cookieParser = require('cookie-parser')
const fs = require('fs');
const mode = "prod"; // "prod || debug"
const app = express();
app.use(express.json())
app.use(cookieParser())
const port = process.env.PORT || 8080
const devPort = 3000
const cookieName = "chessStudies";
const baseFilename = "chessGames";
const unauthFilename = "exampleGames.json";
let users = [{id:1,login:"[email protected]",name:"philmageo",hash:"$2b$10$J1hZVj6AGwmHoaY3F31/9OR75lldnlE5diARa/79Xl25EKVumMeuS",sessionId:"azertyuiop"}];
let chessGames = [];
var whitelist = [ 'http://localhost:'+port,'http://localhost:'+devPort]; //white list consumers : on devPort it works like an api
var corsOptions = {
origin: function (origin, callback) {
if (!origin || whitelist.indexOf(origin) !== -1) {
callback(null, true);
console.log("white list");
} else {
callback(null, false);
console.log("black list");
}
},
methods: ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS'],
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
credentials: false, //Credentials are cookies, authorization headers or TLS client certificates.
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'device-remember-token', 'Access-Control-Allow-Origin', 'Origin', 'Accept']
};
app.use(cors(corsOptions)); //adding cors middleware to the express with above configurations
app.use(express.static(__dirname + "/public"));
const loadGames = (file) => {
return new Promise(function (resolve, reject) {
if (!fs.existsSync("./data/" + file)) {
chessGames = [];
fs.writeFile("./data/" + file, JSON.stringify(chessGames), err => {
if (err) {
console.log("Error writing file on creatings games for new user:", err);
result.success = false;
result.message = err.toString();
return;
}
});
resolve(chessGames);
} else {
fs.readFile("./data/" + file, 'utf8', (err, rawdata) => {
if (err) {
console.error(err)
reject(error);
return
}
chessGames = JSON.parse(rawdata);
resolve(chessGames);
})
}
});
}
// const
const readingError = "readingError";
const sessionError = "sessionError";
// -- logout
app.get('/logout',(req,res) => {
let user = checkSession(req);
if(user){
users.forEach((x)=>{
if(x.id === user.id){
x.sessionId = null;
}
});
res.clearCookie(cookieName);
}else{
if(cookieName in req.cookies){
res.clearCookie(cookieName);
}
}
res.redirect('/');
});
// -- login
app.post('/login', (req,res) => {
let result = {"success":false,"message":sessionError};
let auth = req.body; // your JSON
let username = auth.username.trim();
let password = auth.password.trim();
if(cookieName in req.cookies){
res.clearCookie(cookieName);
}
checkPassword(username,password)
.then(function(userId){
let userArr = users.filter((x)=>{
return x.id === userId;
});
let sessionId = (new Date()).getTime().toString(36) + Math.random().toString(36).slice(2);
users.forEach((x)=>{
if(x.id === userId){
x.sessionId = sessionId;
}
});
res.cookie(cookieName, sessionId, {httpOnly: true});
result = {"success":true,"message":"Welcome back "+ userArr[0].name +"!"};
res.send(result)
})
.catch(function(userId){
if(userId != 0){
users.forEach((x)=>{
if(x.id === userId){
x.sessionId = null;
}
});
}
res.send(result)
})
})
const checkPassword = (username,password) => {
return new Promise(function (resolve, reject) {
const isDebug = mode === "debug" ;
let usernameToTest = isDebug ? "philmageo" : username;
let userArr = users.filter((x)=>{
return x.name === usernameToTest;
});
if(userArr.length === 1){
if(isDebug){
resolve(userArr[0].id);
}else{
bcrypt.compare(password, userArr[0].hash)
.then(function(compareResult) {
if(compareResult){
resolve(userArr[0].id);
}else{
reject(userArr[0].id)
}
});
}
}else{
reject(0);
}
});
}
// --- Getting all the games
app.get('/games', (req,res) => {
let session = checkSession(req);
if(session){
let filename = baseFilename + "-" + session.name + ".json";
loadGames(filename)
.then(function (data) {
users.forEach((x)=>{
if(x.id === session.id){
x.games = [... data];
}
});
res.status(200).json(session.games);
})
.catch(function(error){
res.status(200).json({"error" : readingError});
});
}else{
res.status(200).json({"error" : sessionError});
}
})
// --- Getting one game
app.get('/game/:id', (req,res) => {
let session = checkSession(req);
if(session){
const id = parseInt(req.params.id)
let chessGames = JSON.parse(session.games);
const chessGame = chessGames.find(game => game.id === id)
res.status(200).json(chessGame)
}else{
res.status(200).json({"error" : sessionError});
}
})
// --- Deleting one game
app.delete('/game/:id', (req, res) => {
let session = checkSession(req);
if(session){
let result = {"success":true,"message":""};
let id = Number(req.params.id);
let filename = baseFilename + "-" + session.name + ".json";
loadGames(filename)
.then(function (chessGames) {
chessGames = chessGames.filter((game) => {
return game.id !== id;
});
setUserSessionGames(session.id,chessGames)
fs.writeFile("./data/" + filename, JSON.stringify(chessGames), err => {
if (err) {
console.log("Error writing file on delete one:", err);
result.success = false;
result.message = err.toString();
}
});
res.status(200).json(result);
})
.catch(function(error){
res.status(200).json({"error" : readingError});
});
}else{
res.status(200).json({"error" : sessionError});
}
});
// --- posting a new game
app.put('/game', function(req, res){
let session = checkSession(req);
if(session){
let result = {"success":true,"message":""};
let game = req.body; // your JSON
let id = game.id;
let filename = baseFilename + "-" + session.name + ".json";
loadGames(filename)
.then(function (chessGames) {
if(!chessGames || chessGames.length === 0){
chessGame = [];
chessGames.push(game);
}else{
let checkGame = chessGames.filter((x) => {
return x.id === id;
});
if(checkGame.length === 0){
chessGames.push(game);
}else{
chessGames = chessGames.filter((game) => {
return game.id !== id;
});
chessGames.push(game);
}
}
setUserSessionGames(session.id,chessGames)
fs.writeFile("./data/" + filename, JSON.stringify(chessGames), err => {
if (err) {
console.log("Error writing file on adding one:", err);
result.success = false;
result.message = err.toString();
}
});
res.status(200).json(result);
})
.catch(function(error){
res.status(200).json({"error" : readingError});
});
}else{
res.status(200).json({"error" : sessionError});
}
});
app.listen(port, () => {
console.log("Chess server starts at port " + port);
})
function getUserSessionGames(id){
let chessGames = [];
users.forEach((x)=>{
if(x.id === id){
chessGames = [... x.games];
}
});
return chessGames;
}
function setUserSessionGames(id,games){
let chessGames = [];
users.forEach((x)=>{
if(x.id === id){
x.games = [... games];
}
});
}
function checkSession(req) {
if (mode === "debug") {
let userArr = users.filter((x)=>{
return x.name === "philmageo";
});
return userArr.length === 1 ? userArr[0] : false;
} else {
if (cookieName in req.cookies) {
let userArr = users.filter((x) => {
return x.sessionId === req.cookies.chessStudies;
})
return userArr.length === 1 ? userArr[0] : false;
} else {
return false;
}
}
}