backend: proper error messages for creating accounts

Admins will now get proper error messages when they try to create a
new user account and an error occurs.

Changelog: Fixed
This commit is contained in:
Johann150 2022-09-21 17:58:42 +02:00
parent 7ea052aa25
commit 0022a7befb
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -10,6 +10,7 @@ import { UserKeypair } from '@/models/entities/user-keypair.js';
import { usersChart } from '@/services/chart/index.js'; import { usersChart } from '@/services/chart/index.js';
import { UsedUsername } from '@/models/entities/used-username.js'; import { UsedUsername } from '@/models/entities/used-username.js';
import { db } from '@/db/postgre.js'; import { db } from '@/db/postgre.js';
import { ApiError } from '@/server/api/error.js';
import generateUserToken from './generate-native-user-token.js'; import generateUserToken from './generate-native-user-token.js';
export async function signup(opts: { export async function signup(opts: {
@ -23,13 +24,25 @@ export async function signup(opts: {
// Validate username // Validate username
if (!Users.validateLocalUsername(username)) { if (!Users.validateLocalUsername(username)) {
throw new Error('INVALID_USERNAME'); throw new ApiError({
message: 'This username is invalid.',
code: 'INVALID_USERNAME',
id: 'ece89f3c-d845-4d9a-850b-1735285e8cd4',
kind: 'client',
httpStatusCode: 400,
});
} }
if (password != null && passwordHash == null) { if (password != null && passwordHash == null) {
// Validate password // Validate password
if (!Users.validatePassword(password)) { if (!Users.validatePassword(password)) {
throw new Error('INVALID_PASSWORD'); throw new ApiError({
message: 'This password is invalid.',
code: 'INVALID_PASSWORD',
id: 'a941905b-fe7b-43e2-8ecd-50ad3a2287ab',
kind: 'client',
httpStatusCode: 400,
});
} }
// Generate hash of password // Generate hash of password
@ -40,14 +53,22 @@ export async function signup(opts: {
// Generate secret // Generate secret
const secret = generateUserToken(); const secret = generateUserToken();
const duplicateUsernameError = {
message: 'This username is not available.',
code: 'USED_USERNAME',
id: '7ddd595e-6860-4593-93c5-9fdbcb80cd81',
kind: 'client',
httpStatusCode: 409,
};
// Check username duplication // Check username duplication
if (await Users.findOneBy({ usernameLower: username.toLowerCase(), host: IsNull() })) { if (await Users.findOneBy({ usernameLower: username.toLowerCase(), host: IsNull() })) {
throw new Error('DUPLICATED_USERNAME'); throw new ApiError(duplicateUsernameError);
} }
// Check deleted username duplication // Check deleted username duplication
if (await UsedUsernames.findOneBy({ username: username.toLowerCase() })) { if (await UsedUsernames.findOneBy({ username: username.toLowerCase() })) {
throw new Error('USED_USERNAME'); throw new ApiError(duplicateUsernameError);
} }
const keyPair = await new Promise<string[]>((res, rej) => const keyPair = await new Promise<string[]>((res, rej) =>
@ -76,7 +97,7 @@ export async function signup(opts: {
host: IsNull(), host: IsNull(),
}); });
if (exist) throw new Error(' the username is already used'); if (exist) throw new ApiError(duplicateUsernameError);
account = await transactionalEntityManager.save(new User({ account = await transactionalEntityManager.save(new User({
id: genId(), id: genId(),