-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
50 lines (42 loc) · 1.13 KB
/
middleware.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
let passport = require("passport");
let requireAuth = passport.authenticate('jwt', { session: false });
let requireAdmin = function (req, res, next) {
if (!req.user || !req.user.is_admin) {
res.status(401).json({
error: true,
message: "This API requires admin privelages"
});
} else {
next()
}
};
/**
* Helper functions
*/
let sendValidationErrors = function(res, validator) {
let validationError = Object.keys(validator.errors["errors"]).map(function(k) { return validator.errors["errors"][k][0]; })[0];
return res.status(400).json({
error: true,
validationErrors: validator.errors["errors"],
message: validationError
});
}
let sendErrorMessage = function(res, status, message) {
return res.status(status).json({
error: true,
message: message
});
}
const catchAll = function (promise) {
return promise.then(data => {
return [null, data];
})
.catch(err => [err]);
}
module.exports = {
requireAuth,
requireAdmin,
sendValidationErrors,
sendErrorMessage,
catchAll
};