anything-llm/server/utils/http/index.js

35 lines
767 B
JavaScript
Raw Normal View History

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;
}
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,
makeJWT,
decodeJWT,
2023-06-04 04:28:07 +02:00
};