detached 596c40c

feat: Add username uniqueness check during user registration

commit
596c40cf4064b1aeb7bfa2e94faf792961061c58
author
Kit · · 2025-02-17 07:09 UTC
parent
ff2a41e
signature
SSH UnverifiedSHA256:H91DFuEG4jpk5edzLA4aUKkpv4zv9Q339ipRmkph6Tk The key is unknown
1 file · +8 -0
M src/routes/api/register/+server.ts +8 -0
@@ -5,6 +5,7 @@ import { users } from '$lib/db/schema';
55 import { hashPassword } from '$lib/utils/password';
66 import { validateTurnstileToken } from '$lib/utils/turnstile.server';
77 import { createLogger } from '$lib/utils/logger.server';
8+import { eq } from 'drizzle-orm';
89
910 const log = createLogger('register-server');
1011
@@ -122,6 +123,13 @@ export const POST: RequestHandler = async ({ request }) => {
122123 return new Response(JSON.stringify({ error: 'Password must be between 8 and 64 characters' } as RegisterResponseError), { status: 400 });
123124 }
124125
126+ // Check if nickname already exists
127+ const existingUser = await db.select().from(users).where(eq(users.nickname, username));
128+ if (existingUser.length > 0) {
129+ log.warn('Username already exists', { username });
130+ return new Response(JSON.stringify({ error: 'This username is already taken' } as RegisterResponseError), { status: 409 });
131+ }
132+
125133 // Securely hash the user's password.
126134 let hashedPassword: string;
127135 try {
@@ -5,6 +5,7 @@ import { users } from '$lib/db/schema';
5 import { hashPassword } from '$lib/utils/password';5 import { hashPassword } from '$lib/utils/password';
6 import { validateTurnstileToken } from '$lib/utils/turnstile.server';6 import { validateTurnstileToken } from '$lib/utils/turnstile.server';
7 import { createLogger } from '$lib/utils/logger.server';7 import { createLogger } from '$lib/utils/logger.server';
8+import { eq } from 'drizzle-orm';
8 9
9 const log = createLogger('register-server');10 const log = createLogger('register-server');
10 11
@@ -122,6 +123,13 @@ export const POST: RequestHandler = async ({ request }) => {
122 return new Response(JSON.stringify({ error: 'Password must be between 8 and 64 characters' } as RegisterResponseError), { status: 400 });123 return new Response(JSON.stringify({ error: 'Password must be between 8 and 64 characters' } as RegisterResponseError), { status: 400 });
123 }124 }
124 125
126+ // Check if nickname already exists
127+ const existingUser = await db.select().from(users).where(eq(users.nickname, username));
128+ if (existingUser.length > 0) {
129+ log.warn('Username already exists', { username });
130+ return new Response(JSON.stringify({ error: 'This username is already taken' } as RegisterResponseError), { status: 409 });
131+ }
132+
125 // Securely hash the user's password.133 // Securely hash the user's password.
126 let hashedPassword: string;134 let hashedPassword: string;
127 try {135 try {

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 3

Tags 8

↑↓ moveenter openesc close