forked from cs4241-21a/final_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
264 lines (225 loc) · 6.44 KB
/
server.js
File metadata and controls
264 lines (225 loc) · 6.44 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
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
const express = require("express");
const path = require("path");
const { MongoClient, ObjectId } = require("mongodb");
const session = require("express-session");
const mongoUri =
"mongodb+srv://groupAdmin:groupPassword@cluster0.9zsah.mongodb.net/";
const mongoClient = new MongoClient(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoClient.connect((err) => {
console.log("Mongo client connected :)");
});
const app = express();
app.use(
session({ secret: "fortnite", resave: false, saveUninitialized: true })
);
app.use(express.json());
app.use(express.static("build"));
app.use(express.urlencoded({ extended: true }));
/*
===================================================
USER AUTHENTICATION ROUTES
===================================================
POST /login
POST /signup
GET /me
*/
/*
Try to log the user in
Give them a cookie to keep track of them
If invalid login info, respond with 400
*/
app.post("/login", async (req, res) => {
const userInfo = req.body;
const userLookup = await mongoClient
.db("final")
.collection("users")
.findOne(userInfo);
if (!userLookup) {
console.log(`Login failed attempted user/pass:`);
console.log(userInfo);
res.status(400).end();
return;
}
console.log(`Session is now logged in as ${userLookup.username}`);
req.session.username = userLookup.username;
console.log(req.session);
res.json(userLookup);
});
/*
Attempt to create a new user with the specified username/password
If duplicate username, respond with 400
*/
app.post("/signup", async (req, res) => {
const userInfo = req.body;
const existingUser = await mongoClient
.db("final")
.collection("users")
.findOne({ username: req.body.username });
if (existingUser) {
console.log(
`Attempted to sign up with existing username: ${existingUser.username}`
);
res.status(400).end();
return;
}
const insertRes = await mongoClient
.db("final")
.collection("users")
.insertOne({
...userInfo,
workouts: [],
});
console.log(`Added new user with username ${userInfo.username}`);
console.log("Logging in by setting session username");
req.session.username = userInfo.username;
console.log(req.session);
res.json(userInfo);
});
/*
Respond with a json object of the currently logged in user, and all their lifting data
If not logged in, respond with a 401
*/
app.get("/me", async (req, res) => {
if (!req.session.username) {
console.log("Non-logged person tried /me");
res.status(401).end();
return;
}
const userRes = await mongoClient
.db("final")
.collection("users")
.findOne({ username: req.session.username });
if (!userRes) {
console.log("Session has an invalid username (THIS SHOULD NEVER HAPPEN)");
res.status(401).end();
return;
}
console.log(`Reminding user ${req.session.username} of all their data`);
//console.log(userRes);
res.json(userRes);
});
app.delete("/me", (req, res) => {
console.log(`Logging out ${req.session.username}`);
req.session.destroy();
res.status(200).end();
});
// WORKOUT MODIFICATIONS
app.post("/workout", async (req, res) => {
if (!req.session.username) {
console.log("Not even logged in wtf");
res.status(401).end();
}
console.log(`Adding new workout for user ${req.session.username}`);
const time = new Date();
const updateRes = await mongoClient
.db("final")
.collection("users")
.updateOne(
{ username: req.session.username },
{
$push: {
workouts: {
_id: new ObjectId().toHexString(),
name: `New workout ${time.toLocaleString()}`,
movements: [],
},
},
}
);
res.status(200).end();
});
app.patch("/movement", async (req, res) => {
console.log("Received PATCH to /movement");
if (!req.session.username) {
console.log("Not even logged in wtf");
res.status(401).end();
}
console.log(`Modifying movement for user ${req.session.username}`);
console.log(`Modifying movement for workout ${req.query.workout_id}`);
console.log(req.body);
const updateRes = await mongoClient
.db("final")
.collection("users")
.updateOne(
{
username: req.session.username,
"workouts._id": req.query.workout_id,
},
{
$set: {
"workouts.$.movements.$[element]": req.body,
},
},
{
arrayFilters: [{ "element.movementName": req.body.movementName }],
}
);
console.log(updateRes);
res.status(200).end();
});
app.post("/movement", async (req, res) => {
console.log("Received POST to /movement");
if (!req.session.username) {
console.log("Not even logged in wtf");
res.status(401).end();
}
console.log(`Adding new movement for user ${req.session.username}`);
console.log(`Adding new movement for workout ${req.query.workout_id}`);
const updateRes = await mongoClient
.db("final")
.collection("users")
.updateOne(
{
username: req.session.username,
"workouts._id": req.query.workout_id,
},
{ $push: { "workouts.$.movements": req.body } }
);
res.status(200).end();
});
// Although we want express.static, we are using react-router for routing so we only need index.html
// Define all GET routes before this so that we don't accidentaly send index.html when we want something else
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/build/index.html"));
});
/*
Attempt to create a new user with the specified username/password
If duplicate username, respond with 400
*/
app.delete("/workout", async (req, res) => {
//{workoutid:}
//const userInfo = req.body;
/*
const existingUser = await mongoClient
.db("final")
.collection("users")
.findOne({ username: req.session.username });
if (!existingUser) {
res.json(userInfo);
}
*/
console.log("Deleting workout " + req.query._id);
await mongoClient
.db("final")
.collection("users")
.updateOne(
{ username: req.session.username },
{ $pull: { workouts: { _id: req.query._id } } }
);
res.status(200).end();
});
app.delete("/movement", async (req, res) => {
console.log("Deleting movement " + req.query._id);
await mongoClient
.db("final")
.collection("users")
.updateOne(
{ username: req.session.username },
{ $pull: { workouts: { $elemMatch: { _id: req.query._id } } } } //should drop entire movement elem but not workout
);
res.status(200).end();
});
app.listen(3000);