dsql-admin/dsql-app/package-shared/functions/backend/cookies/get-auth-cookie-names.js

36 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-12-08 08:57:48 +00:00
// @ts-check
/**
* # Grab Auth Cookie Names
*
* @param {object} [params]
* @param {string} [params.database]
* @param {string | number} [params.userId]
*
* @returns {{ keyCookieName: string, csrfCookieName: string }}
*/
module.exports = function getAuthCookieNames(params) {
2024-12-06 13:24:26 +00:00
const cookiesPrefix = process.env.DSQL_COOKIES_PREFIX || "dsql_";
const cookiesKeyName = process.env.DSQL_COOKIES_KEY_NAME || "key";
const cookiesCSRFName = process.env.DSQL_COOKIES_CSRF_NAME || "csrf";
2024-12-09 12:27:08 +00:00
const targetDatabase =
params?.database ||
process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/, "");
2024-12-08 08:57:48 +00:00
let keyCookieName = cookiesPrefix;
if (params?.userId) keyCookieName += `user_${params.userId}_`;
2024-12-09 12:27:08 +00:00
if (targetDatabase) keyCookieName += `${targetDatabase}_`;
2024-12-08 08:57:48 +00:00
keyCookieName += cookiesKeyName;
let csrfCookieName = cookiesPrefix;
if (params?.userId) csrfCookieName += `user_${params.userId}_`;
2024-12-09 12:27:08 +00:00
if (targetDatabase) csrfCookieName += `${targetDatabase}_`;
2024-12-08 08:57:48 +00:00
csrfCookieName += cookiesCSRFName;
2024-12-06 13:24:26 +00:00
return {
keyCookieName,
csrfCookieName,
};
};