Add errors to user registration

This commit is contained in:
Jesse Wierzbinski 2023-09-12 20:06:12 -10:00
parent 8d472d3d45
commit b96fe8116e
No known key found for this signature in database
GPG key ID: F9A1E418934E40B0
2 changed files with 3028 additions and 2 deletions

View file

@ -1,4 +1,6 @@
import { getConfig } from "@config";
import { jsonResponse } from "@response";
import { tempmailDomains } from "@tempmail";
import { User } from "~database/entities/User";
/**
@ -107,10 +109,14 @@ export default async (req: Request): Promise<Response> => {
});
// Check if email is blocked
if (config.validation.email_blacklist.includes(body.email))
if (
config.validation.email_blacklist.includes(body.email) ||
(config.validation.blacklist_tempmail &&
tempmailDomains.domains.includes(body.email.split("@")[1]))
)
errors.details.email.push({
error: "ERR_BLOCKED",
description: `is blocked`,
description: `is from a blocked email provider`,
});
// Check if agreement is accepted
@ -120,6 +126,24 @@ export default async (req: Request): Promise<Response> => {
description: `must be accepted`,
});
// If any errors are present, return them
if (Object.values(errors.details).some(value => value.length > 0)) {
// Error is something like "Validation failed: Password can't be blank, Username must contain only letters, numbers and underscores, Agreement must be accepted"
const errorsText = Object.entries(errors.details)
.map(
([name, errors]) =>
`${name} ${errors
.map(error => error.description)
.join(", ")}`
)
.join(", ");
return jsonResponse({
error: `Validation failed: ${errorsText}`,
details: errors.details,
});
}
// TODO: Check if locale is valid
const newUser = new User();

3002
utils/tempmail.ts Normal file

File diff suppressed because it is too large Load diff