2021-06-07 01:26:12 +08:00
|
|
|
const MonkeyError = require("../handlers/error");
|
2021-06-07 00:32:37 +08:00
|
|
|
const { verifyIdToken } = require("../handlers/auth");
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
async authenticateRequest(req, res, next) {
|
|
|
|
try {
|
|
|
|
const { authorization } = req.headers;
|
2021-06-07 01:26:12 +08:00
|
|
|
if (!authorization)
|
|
|
|
return next(
|
|
|
|
new MonkeyError(404, "Unauthorized", "No authorization header")
|
|
|
|
);
|
2021-06-07 00:32:37 +08:00
|
|
|
const token = authorization.split(" ");
|
2021-06-17 03:56:00 +08:00
|
|
|
if (token[0].trim() !== "Bearer")
|
2021-06-07 01:26:12 +08:00
|
|
|
return next(
|
|
|
|
new MonkeyError(
|
|
|
|
400,
|
|
|
|
"Invalid Token",
|
|
|
|
"Only bearer tokens are accepted."
|
|
|
|
)
|
|
|
|
);
|
2021-06-07 00:32:37 +08:00
|
|
|
req.decodedToken = await verifyIdToken(token[1]);
|
|
|
|
return next();
|
|
|
|
} catch (e) {
|
|
|
|
return next(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|