anything-llm/server/utils/http/index.js
Timothy Carambat 6b6007f9ad
Enable the system owner to be able to update the system wide password and secret (#156)
* Enable the system owner to be able to update the system wide password and secret

* lint and cleanup
2023-07-20 15:25:47 -07:00

35 lines
787 B
JavaScript

process.env.NODE_ENV === "development"
? require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` })
: require("dotenv").config();
const JWT = require("jsonwebtoken");
function reqBody(request) {
return typeof request.body === "string"
? JSON.parse(request.body)
: request.body;
}
function queryParams(request) {
return request.query;
}
function makeJWT(info = {}, expiry = "30d") {
if (!process.env.JWT_SECRET)
throw new Error("Cannot create JWT as JWT_SECRET is unset.");
return JWT.sign(info, process.env.JWT_SECRET, { expiresIn: expiry });
}
function decodeJWT(jwtToken) {
try {
return JWT.verify(jwtToken, process.env.JWT_SECRET);
} catch {}
return { p: null };
}
module.exports = {
reqBody,
queryParams,
makeJWT,
decodeJWT,
};