Files
Checkmate/Server/utils/utils.js
T

35 lines
817 B
JavaScript
Raw Normal View History

/**
* Converts a request body parameter to a boolean.
* @param {string | boolean} value
* @returns {boolean}
*/
const ParseBoolean = (value) => {
2024-10-17 10:42:57 +08:00
if (value === true || value === "true") {
return true;
} else if (
value === false ||
value === "false" ||
value === null ||
value === undefined
) {
return false;
}
};
const getTokenFromHeaders = (headers) => {
2024-10-17 10:42:57 +08:00
const authorizationHeader = headers.authorization;
if (!authorizationHeader) throw new Error("No auth headers");
2024-10-17 10:42:57 +08:00
const parts = authorizationHeader.split(" ");
if (parts.length !== 2 || parts[0] !== "Bearer")
throw new Error("Invalid auth headers");
2024-10-17 10:42:57 +08:00
return parts[1];
};
const tokenType = Object.freeze({
2024-10-17 10:42:57 +08:00
ACCESS_TOKEN: "Access token",
REFRESH_TOKEN: "Refresh token",
});
2024-10-17 10:42:57 +08:00
export { ParseBoolean, getTokenFromHeaders, tokenType };