-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
214 lines (187 loc) · 5.4 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
"use strict";
// imports
const express = require("express");
const morgan = require("morgan");
const { check, validationResult } = require("express-validator");
const cors = require("cors");
const dao = require("./qaDao");
const userDao = require("./userDao");
// Passport-related imports
const passport = require("passport");
const LocalStrategy = require("passport-local");
const session = require("express-session");
// init
const app = express();
const port = 3001;
// set up middlewares
app.use(express.json());
app.use(morgan("dev"));
const corsOptions = {
origin: "http://localhost:5173",
optionsSuccessStatus: 200,
credentials: true,
};
app.use(cors(corsOptions));
// Passport: set up local strategy
passport.use(
new LocalStrategy(async function verify(username, password, cb) {
const user = await userDao.getUser(username, password);
if (!user) return cb(null, false, "Incorrect username or password.");
return cb(null, user);
})
);
passport.serializeUser(function (user, cb) {
cb(null, user);
});
passport.deserializeUser(function (user, cb) {
// this user is id + email + name
return cb(null, user);
// if needed, we can do extra check here (e.g., double check that the user is still in the database, etc.)
});
const isLoggedIn = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
return res.status(401).json({ error: "Not authorized" });
};
app.use(
session({
secret: "shhhhh... it's a secret!",
resave: false,
saveUninitialized: false,
})
);
app.use(passport.authenticate("session"));
/* ROUTES */
// GET /api/questions
app.get("/api/questions", (request, response) => {
dao
.listQuestions()
.then((questions) => response.json(questions))
.catch(() => response.status(500).end());
});
// GET /api/questions/<id>
app.get("/api/questions/:id", async (req, res) => {
try {
const question = await dao.getQuestion(req.params.id);
if (question.error) res.status(404).json(question);
else res.json(question);
} catch {
res.status(500).end();
}
});
// GET /api/questions/<id>/answers
app.get("/api/questions/:id/answers", async (req, res) => {
try {
const answers = await dao.listAnswersOf(req.params.id);
res.json(answers);
} catch {
res.status(500).end();
}
});
// POST /api/questions/<id>/answers
app.post(
"/api/questions/:id/answers",
[
check("text").notEmpty(),
check("author").notEmpty(),
check("score").isNumeric(),
check("date").isDate({ format: "YYYY-MM-DD", strictMode: true }),
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
const newAnswer = req.body;
const questionId = req.params.id;
try {
const id = await dao.addAnswer(newAnswer, questionId);
res.status(201).location(id).end();
} catch (e) {
console.error(`ERROR: ${e.message}`);
res.status(503).json({ error: "Impossible to create the answer." });
}
}
);
// PUT /api/answers/<id>
app.put(
"/api/answers/:id",
[
check("text").notEmpty(),
check("author").notEmpty(),
check("score").isNumeric(),
check("date").isDate({ format: "YYYY-MM-DD", strictMode: true }),
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
const answerToUpdate = req.body;
const answerId = req.params.id;
try {
await dao.updateAnswer(answerToUpdate, answerId);
res.status(200).end();
} catch {
res
.status(503)
.json({ error: `Impossible to update answer #${answerId}.` });
}
}
);
// POST /api/answers/<id>/vote
app.post(
"/api/answers/:id/vote",
isLoggedIn,
[check("vote").notEmpty()],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
const answerId = req.params.id;
try {
const num = await dao.voteAnswer(answerId, req.body.vote);
if (num === 1) res.status(204).end();
else throw new Error(`Error in casting a vote for answer #${answerId}`);
} catch (e) {
res.status(503).json({ error: e.message });
}
}
);
// POST /api/sessions
app.post("/api/sessions", function (req, res, next) {
passport.authenticate("local", (err, user, info) => {
if (err) return next(err);
if (!user) {
// display wrong login messages
return res.status(401).send(info);
}
// success, perform the login
req.login(user, (err) => {
if (err) return next(err);
// req.user contains the authenticated user, we send all the user info back
return res.status(201).json(req.user);
});
})(req, res, next);
});
/* If we aren't interested in sending error messages... */
/*app.post('/api/sessions', passport.authenticate('local'), (req, res) => {
// req.user contains the authenticated user, we send all the user info back
res.status(201).json(req.user);
});*/
// GET /api/sessions/current
app.get("/api/sessions/current", (req, res) => {
if (req.isAuthenticated()) {
res.json(req.user);
} else res.status(401).json({ error: "Not authenticated" });
});
// DELETE /api/session/current
app.delete("/api/sessions/current", (req, res) => {
req.logout(() => {
res.end();
});
});
// start the server
app.listen(port, () => "API server started");