This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
337 lines (297 loc) · 11.5 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Import modules
const express = require("express");
const path = require("path");
const app = express();
const sessionObj = require("express-session");
const port = 3000;
const crudOperations = require("./crudOperationFunctions.js");
const passport = require("passport");
const dotenv = require("dotenv");
dotenv.config();
// Middlewares
// Middle ware for parsing JSON
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
// Middleware to create in-memory session store
// In-memory vs persistent datastore (Database based)
// Session data is lost when the server is stopped with in-memory store
// Session data is not lost when the server is stopped
// Below is the configuration for in-memory datastore
// For details on Options used for this middleware check out - https://www.npmjs.com/package/express-session
app.use(
sessionObj({
secret: "secret",
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 1000 * 60 * 10,
},
})
);
// // Initialize passport auth middleware
app.use(passport.initialize());
app.use(passport.session());
// // Initialise the "Local" Strategy for passport authentication
const passportAuth = require("./passportAuth.js");
const middleware = require("./middleware");
const authenticateInternal = require("./authenticationMiddleWareInternal");
passportAuth.passportInit();
// // *********** Below are all the endpoints where requests are sent ( GET, PUT, DELETE, POST ) ***********
app.get("/", (req, res) => {
// TO DO - Add Authentication for this route . If the user is authenticated redirect to the dashboard else send the login.html page
// Hints - Have Look inside req object for the Authentication method.
// Based on return value of that method: If true: redirect response(res) to /dashboard else send login.html in response(res).
res.sendFile("login.html", { root: path.join(__dirname, "views") });
});
app.get("/login", (req, res) => {
// TO DO - Add Authentication for this route . If the user is authenticated redirect to the dashboard else send the login.html page
// // Hints - Have Look inside req object for the Authentication method.
res.sendFile("login.html", { root: path.join(__dirname, "views") });
});
app.get("/create-user", (req, res) => {
res.sendFile("create_user.html", { root: path.join(__dirname, "views") });
});
// TO DO - Add Authentication for this route which does the following:
// - If the user is authenticated then return update users page or else redirect to login page
// 1. Add a middleware to handle authentication. Look at how middlewares are added to a route.
// 2. Good Part - The middleware files have been already created for you.
// 3. Figure out based on funtionality which will be appropriate to be used here.
app.get("/update-user", (req, res) => {
res.sendFile("./views/update_user.html", { root: __dirname });
});
// TO DO - Add Authentication for this route . If the user is authenticated then return dashboard page or else redirect to login page
// Add a middleware to handle authentication.
// The middleware files have been already created for you.
// Figure out based on funtionality which will be appropriate to be used here.
app.get("/dashboard", (req, res) => {
res.sendFile("./views/dashboard.html", { root: __dirname });
});
app.get("/logout", function (req, res, next) {
req.logout(function (err) {
if (err) {
console.log(err);
return next(err);
}
console.log("Redirecting!");
res.redirect("/");
});
});
// TO DO - Complete the route to return the Prompt Lists.
// 1. Here the middleware has been added for you which checks if you are authenticated or not. If yes, continues with execution else sends response as { "msg": false }
// 2. Look out inside crudOperations file. Use a function that returns you the Prompt List.
// 3. Once found look what needs to be passed as per the function signature. Maybe you can extract that required argument from request obj (req)?
// 4. Handle the value returned by the function (say data) as follows:
// 4.1 If data is not null and not false, return a json response {msg: data}
// 4.2 Else return {msg: []}
app.get("/get_prompt_list", authenticateInternal(), async (req, res) => {
try {
// WRITE
// YOUR
// CODE
// HERE.
} catch (error) {
console.error(error);
res.json({
msg: "There was some issue with fetching prompt list. Check server logs",
});
}
});
app.get("/get_prompt_id", authenticateInternal(), async (req, res) => {
try {
const { email } = req.user;
const data = await crudOperations.getPromptId(email);
if (data != null && data !== false) {
// console.log("Fetched Data Successfully");
res.json({ msg: data[0].id });
} else {
res.json({ msg: [] });
}
} catch (error) {
console.error(error);
res.json({
msg: "There was some issue with fetching prompt ID. Check server logs",
});
}
});
app.post("/create_user", async (req, res) => {
try {
// Parse the data from the request
let { firstname, lastname, emailId, password, role } = req.body;
// Check if user exists or not
const userExists = await crudOperations.checkUser(
emailId,
firstname,
lastname,
password,
role
);
console.log(userExists);
if (userExists !== null) {
if (userExists === false) {
// Call the operation to create the user
// Password Encrypted at dbFunctions.js
const createUserResult = await crudOperations.createUser(
firstname,
lastname,
emailId,
password,
role
);
if (createUserResult === true) {
console.log(`User Created with email id ${emailId}`);
res.json({ msg: true });
} else {
res.json({ msg: false });
}
} else {
// If userExists is not false, send the data to frontend
console.log(`Problems Creating a new user as:- ${userExists}`);
res.json({ msg: false });
}
} else {
console.log("Error while checking user existence.");
res.json({ msg: false });
}
} catch (error) {
console.error("Error while creating user:", error);
res.json({ msg: "Error While Creating User" });
}
});
app.post("/login", passport.authenticate("local"), (req, res) => {
res.redirect("/dashboard");
});
// TO DO - Add Authentication for this route. If not authenticated respond with this json - {"msg" : false}
// 1. Add a middleware to handle authentication
// 2. The middleware files have been already created for you.
// 3. Figure out based on funtionality which will be appropriate to be used here.
// 4. Look out at crudOperations file to look at appropriate function and check its function signature.
// 5. Now pass the extracted fields from the requests' body to the function.
// 6. Handle the result as following:
// 6.1. If result is true return a json response as {msg: true}
// 6.2. Else return {msg: false}
app.post("/update_user", async (req, res) => {
try {
let { firstname, lastname, emailId } = req.body;
// const result = replaceFunctionHere(a, b, c);
// WRITE
// YOUR
// CODE
// HERE.
} catch (error) {
console.log(error);
res.json({
msg: "Error While Updating User. See Server Logs for Details",
});
}
});
// TO DO - Add Authentication for this route. If not authenticated respond with this json - {"msg" : false}
// 1. Add a middleware to handle authentication
// 2. The middleware files have been already created for you.
// 3. Figure out based on funtionality which will be appropriate to be used here.
app.post("/create_user_prompt", async (req, res) => {
try {
const { prompt } = req.body;
const { name, email } = req.user;
const result = await crudOperations.createPrompt(name, prompt, email);
if (result === true) {
console.log(`Added prompt for email id ${email}`);
res.json({ msg: true });
} else {
console.log(`Unable to add new prompt`);
console.log(result);
res.json({ msg: result });
}
} catch (error) {
console.error(error);
res.json({
msg: "Error While Creating New Prompt. See Server Logs for Details",
});
}
});
// TO DO - Add Authentication for this route. If not authenticated respond with this json - {"msg" : false}
// 1. Add a middleware to handle authentication
// 2. The middleware files have been already created for you.
// 3. Figure out based on funtionality which will be appropriate to be used here.
app.put("/update_user_prompt/:id", replaceHere(), async (req, res) => {
try {
// TO DO - Add Authentication for this route
// Check if the user has permissions (proper role) to approve / complete the request
// If user has permissions then proceed else return json: {"msg": false}
const id = req.params.id;
const updatedPrompt = req.body.prompt; // Extract the updated prompt from the request body
const isUpdated = await crudOperations.updatePrompt(id, updatedPrompt);
if (isUpdated) {
console.log(`Updated Prompt for id -> ${id}`);
console.log(req.body);
res.json({ msg: true });
} else {
console.log(`Failed to update request for id -> ${id}`);
res.json({ msg: false });
}
} catch (error) {
console.error(error);
res.status(500).json({
msg: "There was some issue in updating the prompt. Check server logs.",
});
}
});
// TO DO - Add Authentication for this route. If not authenticated respond with this json - {"msg" : false}
// 1. Add a middleware to handle authentication
// 2. The middleware files have been already created for you.
// 3. Figure out based on funtionality which will be appropriate to be used here.
app.delete("/delete_user_prompt/:id", replaceHere(), async (req, res) => {
try {
// TO DO - Add Authentication for this route
// Check if the user has permission's (proper role) to cancel the request
// If user has permissions then proceed else return json: {"msg": false}
const cancellationResult = await crudOperations.deletePrompt(req.params.id);
if (cancellationResult === true) {
console.log(`Deleted prompt for id -> ${req.params.id}`);
res.json({ msg: true });
} else {
console.log(cancellationResult);
res.json({ msg: false });
}
} catch (error) {
console.error(error);
res.json({
msg: "There was an issue in deleting the prompt. Check server logs",
});
}
});
app.delete("/delete_all_prompts", authenticateInternal(), async (req, res) => {
try {
const cancellationResult = await crudOperations.deleteAllPrompts();
if (cancellationResult === true) {
res.json({ msg: true });
} else {
console.log(cancellationResult);
res.json({ msg: false });
}
} catch (error) {
console.error(error);
res.json({
msg: "There was an issue in deleting the prompt. Check server logs",
});
}
});
app.delete("/delete_all_users", authenticateInternal(), async (req, res) => {
try {
const cancellationResult = await crudOperations.deleteAllUsers();
if (cancellationResult === true) {
res.json({ msg: true });
} else {
console.log(cancellationResult);
res.json({ msg: false });
}
} catch (error) {
console.error(error);
res.json({
msg: "There was an issue in deleting the prompt. Check server logs",
});
}
});
// Application setup / entry to create a server on a port which is given as an argument
app.listen(port, () => {
console.log(`Server Up and Listening at http://localhost:${port}/`);
});