directus-extension-disallow-user-registration
Version:
An extension for Directus that disallows the setting of public user registrations to enhance security and prevent spam accounts.
94 lines (86 loc) • 4.33 kB
JavaScript
// src/create-error.ts
var createError = (code, message, status = 500) => {
return class extends Error {
name = "DirectusError";
extensions;
code = code.toUpperCase();
status = status;
constructor(extensions, options) {
const msg = typeof message === "string" ? message : message(extensions);
super(msg, options);
this.extensions = extensions;
}
toString() {
return `${this.name} [${this.code}]: ${this.message}`;
}
};
};
// src/codes.ts
var ErrorCode = /* @__PURE__ */ ((ErrorCode2) => {
ErrorCode2["ContainsNullValues"] = "CONTAINS_NULL_VALUES";
ErrorCode2["ContentTooLarge"] = "CONTENT_TOO_LARGE";
ErrorCode2["Forbidden"] = "FORBIDDEN";
ErrorCode2["IllegalAssetTransformation"] = "ILLEGAL_ASSET_TRANSFORMATION";
ErrorCode2["Internal"] = "INTERNAL_SERVER_ERROR";
ErrorCode2["InvalidCredentials"] = "INVALID_CREDENTIALS";
ErrorCode2["InvalidForeignKey"] = "INVALID_FOREIGN_KEY";
ErrorCode2["InvalidIp"] = "INVALID_IP";
ErrorCode2["InvalidOtp"] = "INVALID_OTP";
ErrorCode2["InvalidPayload"] = "INVALID_PAYLOAD";
ErrorCode2["InvalidProvider"] = "INVALID_PROVIDER";
ErrorCode2["InvalidProviderConfig"] = "INVALID_PROVIDER_CONFIG";
ErrorCode2["InvalidQuery"] = "INVALID_QUERY";
ErrorCode2["InvalidToken"] = "INVALID_TOKEN";
ErrorCode2["LimitExceeded"] = "LIMIT_EXCEEDED";
ErrorCode2["MethodNotAllowed"] = "METHOD_NOT_ALLOWED";
ErrorCode2["NotNullViolation"] = "NOT_NULL_VIOLATION";
ErrorCode2["OutOfDate"] = "OUT_OF_DATE";
ErrorCode2["RangeNotSatisfiable"] = "RANGE_NOT_SATISFIABLE";
ErrorCode2["RecordNotUnique"] = "RECORD_NOT_UNIQUE";
ErrorCode2["RequestsExceeded"] = "REQUESTS_EXCEEDED";
ErrorCode2["RouteNotFound"] = "ROUTE_NOT_FOUND";
ErrorCode2["ServiceUnavailable"] = "SERVICE_UNAVAILABLE";
ErrorCode2["TokenExpired"] = "TOKEN_EXPIRED";
ErrorCode2["UnexpectedResponse"] = "UNEXPECTED_RESPONSE";
ErrorCode2["UnprocessableContent"] = "UNPROCESSABLE_CONTENT";
ErrorCode2["UnsupportedMediaType"] = "UNSUPPORTED_MEDIA_TYPE";
ErrorCode2["UserSuspended"] = "USER_SUSPENDED";
ErrorCode2["ValueOutOfRange"] = "VALUE_OUT_OF_RANGE";
ErrorCode2["ValueTooLong"] = "VALUE_TOO_LONG";
return ErrorCode2;
})(ErrorCode || {});
/**
* This extension disables the public registration.
*
* @param {Object} param - The parameter object.
* @param {Function} param.filter - The filter function to register the filters.
*/
// Define a custom error for invalid payloads when public registration is not allowed
const InvalidPayloadError = createError(ErrorCode.InvalidPayload, 'Public registration not allowed', 500);
var index = ({filter}) => {
/**
* Disables public registration by setting the `public_registration` property to `false`
* if the `PUBLIC_REGISTRATION_ALLOWED` environment variable is not set or is set to `false`.
* Throws an error if public registration is attempted and `PUBLIC_REGISTRATION_THROW_ERROR` is set to `true`.
*
* @param {Object} input - The input settings object.
* @returns {Object} - The modified settings object with `public_registration` set to `false`
* if `PUBLIC_REGISTRATION_ALLOWED` is not set or is set to `false`, otherwise the original input.
* @throws {InvalidPayloadError} - If public registration is attempted and `PUBLIC_REGISTRATION_THROW_ERROR` is set to `true`.
*/
const disablePublicRegistration = input => {
const isPublicRegistrationAllowed = (process.env.PUBLIC_REGISTRATION_ALLOWED === 'true' || process.env.PUBLIC_REGISTRATION_ALLOWED === true);
if (
!isPublicRegistrationAllowed
&& input.public_registration
&& (process.env.PUBLIC_REGISTRATION_THROW_ERROR === 'true' || process.env.PUBLIC_REGISTRATION_THROW_ERROR === true)) {
throw new InvalidPayloadError();
}
// Overwrite the public_registration property if public registration is not allowed
return isPublicRegistrationAllowed ? input : {...input, public_registration: false};
};
// Register the disablePublicRegistration function to the 'settings.create' and 'settings.update' filters
filter('settings.create', disablePublicRegistration);
filter('settings.update', disablePublicRegistration);
};
export { index as default };