mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-05 06:20:10 +01:00
62e3f62e82
* Add Auth protection for cloud-based or private instances * skip check on local dev
35 lines
767 B
JavaScript
35 lines
767 B
JavaScript
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;
|
|
|
|
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 (!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;
|
|
}
|
|
|
|
module.exports = {
|
|
reqBody,
|
|
queryParams,
|
|
makeJWT,
|
|
decodeJWT,
|
|
};
|