Rename server => modules

This commit is contained in:
Michael Jackson
2018-07-31 10:13:26 -07:00
parent 135da0fdc5
commit bef8b2ebee
104 changed files with 13 additions and 13 deletions

View File

@ -0,0 +1,41 @@
const AuthAPI = require("../AuthAPI");
const ReadMethods = { GET: true, HEAD: true };
/**
* Sets req.user from the payload in the auth token in the request.
*/
function userToken(req, res, next) {
if (req.user) {
return next();
}
const token = (ReadMethods[req.method] ? req.query : req.body).token;
if (!token) {
req.user = null;
return next();
}
AuthAPI.verifyToken(token).then(
payload => {
req.user = payload;
next();
},
error => {
if (error.name === "JsonWebTokenError") {
res.status(403).send({
error: `Bad auth token: ${error.message}`
});
} else {
console.error(error);
res.status(500).send({
error: "Unable to verify auth"
});
}
}
);
}
module.exports = userToken;