2023-06-09 20:27:27 +02:00
|
|
|
process.env.NODE_ENV === "development"
|
|
|
|
? require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` })
|
|
|
|
: require("dotenv").config();
|
|
|
|
const JWT = require("jsonwebtoken");
|
|
|
|
const SECRET = process.env.JWT_SECRET;
|
|
|
|
|
2023-06-04 04:28:07 +02:00
|
|
|
function reqBody(request) {
|
2023-06-08 06:31:35 +02:00
|
|
|
return typeof request.body === "string"
|
2023-06-04 04:28:07 +02:00
|
|
|
? JSON.parse(request.body)
|
|
|
|
: request.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
function queryParams(request) {
|
|
|
|
return request.query;
|
|
|
|
}
|
|
|
|
|
2023-06-09 20:27:27 +02:00
|
|
|
function makeJWT(info = {}, expiry = "30d") {
|
|
|
|
if (!SECRET) throw new Error("Cannot create JWT as JWT_SECRET is unset.");
|
|
|
|
return JWT.sign(info, SECRET, { expiresIn: expiry });
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeJWT(jwtToken) {
|
|
|
|
try {
|
|
|
|
return JWT.verify(jwtToken, SECRET);
|
|
|
|
} catch {}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-06-04 04:28:07 +02:00
|
|
|
module.exports = {
|
|
|
|
reqBody,
|
|
|
|
queryParams,
|
2023-06-09 20:27:27 +02:00
|
|
|
makeJWT,
|
|
|
|
decodeJWT,
|
2023-06-04 04:28:07 +02:00
|
|
|
};
|