-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
44 lines (35 loc) · 1.29 KB
/
utils.js
File metadata and controls
44 lines (35 loc) · 1.29 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
const { check, validationResult } = require('express-validator');
const csrf = require("csurf");
const csrfProtection = csrf({ cookie: true });
const asyncHandler = (handler) => (req, res, next) => handler(req, res, next).catch(next);
const handleValidationErrors = (req, res, next) => {
const validationErrors = validationResult(req);
// If the validation errors are not empty,
if (!validationErrors.isEmpty()) {
// Generate an array of error messages
const errors = validationErrors.array().map((err) => err.msg);
// Generate a new `400 Bad request.` Error object
// and invoke the next function passing in `err`
// to pass control to the global error handler.
const err = Error("Bad request.");
err.status = 400;
err.title = "Bad request.";
err.errors = errors;
return next(err);
}
// Invoke the next middlware function
next();
};
const isAuthorized = (req, res, resource) => ((res.locals.user) && (resource.userId === res.locals.user.id));
const styleResources = (resources, size) => {
resources.forEach((resource, i) => {
resource.colorIndex = i % size;
});
}
module.exports = {
asyncHandler,
handleValidationErrors,
csrfProtection,
isAuthorized,
styleResources,
};