Skip to content

Commit 4ebcfe6

Browse files
committed
feat: get all users to check if username already exists, add warning if so
1 parent cc27ab5 commit 4ebcfe6

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

apps/web/src/routes/_app/admin/users/create.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { estimatePasswordStrength } from '@douglasneuroinformatics/libpasswd';
44
import { Form, Heading } from '@douglasneuroinformatics/libui/components';
55
import { useTranslation } from '@douglasneuroinformatics/libui/hooks';
66
import { $BasePermissionLevel, $CreateUserData } from '@opendatacapture/schemas/user';
7-
import type { CreateUserData } from '@opendatacapture/schemas/user';
7+
import type { CreateUserData, User } from '@opendatacapture/schemas/user';
88
import { createFileRoute, useNavigate } from '@tanstack/react-router';
99
import { z } from 'zod/v4';
1010

1111
import { PageHeader } from '@/components/PageHeader';
1212
import { useCreateUserMutation } from '@/hooks/useCreateUserMutation';
1313
import { groupsQueryOptions, useGroupsQuery } from '@/hooks/useGroupsQuery';
14+
import axios from 'axios';
1415

1516
const RouteComponent = () => {
1617
const { t } = useTranslation();
@@ -136,7 +137,7 @@ const RouteComponent = () => {
136137
groupIds: z.set(z.string()).optional(),
137138
confirmPassword: z.string().min(1)
138139
})
139-
.check((ctx) => {
140+
.check(async (ctx) => {
140141
if (!estimatePasswordStrength(ctx.value.password).success) {
141142
ctx.issues.push({
142143
code: 'custom',
@@ -155,6 +156,19 @@ const RouteComponent = () => {
155156
path: ['confirmPassword']
156157
});
157158
}
159+
160+
const existingUsers = await axios.get(`/v1/users`);
161+
if (existingUsers.data) {
162+
const usersList = existingUsers.data as Omit<User, 'hashedpassword'>[];
163+
164+
if (usersList.some((usersList) => usersList.username === ctx.value.username))
165+
ctx.issues.push({
166+
code: 'custom',
167+
input: ctx.value.username,
168+
message: 'Username already exists',
169+
path: ['username']
170+
});
171+
}
158172
})}
159173
onSubmit={(data) => handleSubmit({ ...data, groupIds: Array.from(data.groupIds ?? []) })}
160174
/>

0 commit comments

Comments
 (0)