fix: usernames with whitespace

This commit is contained in:
Face 2025-06-01 20:38:29 +03:00
parent 2663850019
commit be7cdbf0f8
2 changed files with 15 additions and 8 deletions

View file

@ -8,11 +8,11 @@ import { MAX_FILE_SIZE } from '$lib/data/constants';
import { isNameAppropriate } from '$lib/server/moderation'; import { isNameAppropriate } from '$lib/server/moderation';
async function validateInputs(name: string, bio: string, username: string, avatarFile: File | null) { async function validateInputs(name: string, bio: string, username: string, avatarFile: File | null) {
if (name && name.length < 1) { if (name && name.trim().length < 1) {
throw error(400, 'Name cannot be empty'); throw error(400, 'Name cannot be empty');
} }
if (name && !(await isNameAppropriate(name))) { if (name && !(await isNameAppropriate(name.trim()))) {
throw error(400, 'Name contains inappropriate content'); throw error(400, 'Name contains inappropriate content');
} }
@ -54,9 +54,9 @@ export async function POST({ request }) {
} }
const formData = await request.formData(); const formData = await request.formData();
const name = formData.get('name') as string; const name = (formData.get('name') as string)?.trim();
const bio = formData.get('bio') as string; const bio = formData.get('bio') as string;
const username = (formData.get('username') as string)?.toLowerCase(); const username = (formData.get('username') as string)?.toLowerCase().trim();
const avatarFile = formData.get('avatar') as File | null; const avatarFile = formData.get('avatar') as File | null;
await validateInputs(name, bio, username, avatarFile); await validateInputs(name, bio, username, avatarFile);

View file

@ -5,16 +5,23 @@ import { eq } from 'drizzle-orm';
import { isNameAppropriate } from '$lib/server/moderation'; import { isNameAppropriate } from '$lib/server/moderation';
export async function GET({ url }) { export async function GET({ url }) {
const username = url.searchParams.get('username')?.toLowerCase(); const username = url.searchParams.get('username')?.toLowerCase().trim();
if (!username) { if (!username) {
return json({ available: false }); return json({ available: false });
} }
if (username.length < 3 || username.length > 30) {
return json({
available: false,
reason: 'Username must be between 3 and 30 characters'
});
}
const alphanumericRegex = /^[a-z0-9_]+$/; const alphanumericRegex = /^[a-z0-9_]+$/;
if (!alphanumericRegex.test(username)) { if (!alphanumericRegex.test(username)) {
return json({ return json({
available: false, available: false,
reason: 'Username must contain only lowercase letters, numbers, and underscores' reason: 'Username must contain only lowercase letters, numbers, and underscores'
}); });
} }