refactor: update domain validation to use appConfig for allowed domains

This commit is contained in:
Danny Avila 2025-08-18 00:23:45 -04:00
parent 677481dde6
commit 50bd6d3a02
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
8 changed files with 43 additions and 61 deletions

View file

@ -1,10 +1,9 @@
const { getCustomConfig } = require('~/server/services/Config');
/**
* @param {string} email
* @param {string[]} [allowedDomains]
* @returns {Promise<boolean>}
*/
async function isEmailDomainAllowed(email) {
async function isEmailDomainAllowed(email, allowedDomains) {
if (!email) {
return false;
}
@ -15,14 +14,13 @@ async function isEmailDomainAllowed(email) {
return false;
}
const customConfig = await getCustomConfig();
if (!customConfig) {
if (!allowedDomains) {
return true;
} else if (!customConfig?.registration?.allowedDomains) {
} else if (!Array.isArray(allowedDomains) || !allowedDomains.length) {
return true;
}
return customConfig.registration.allowedDomains.includes(domain);
return allowedDomains.includes(domain);
}
/**
@ -65,16 +63,14 @@ function normalizeDomain(domain) {
/**
* Checks if the given domain is allowed. If no restrictions are set, allows all domains.
* @param {string} [domain]
* @param {string[]} [allowedDomains]
* @returns {Promise<boolean>}
*/
async function isActionDomainAllowed(domain) {
async function isActionDomainAllowed(domain, allowedDomains) {
if (!domain || typeof domain !== 'string') {
return false;
}
const customConfig = await getCustomConfig();
const allowedDomains = customConfig?.actions?.allowedDomains;
if (!Array.isArray(allowedDomains) || !allowedDomains.length) {
return true;
}