@@ -4,13 +4,14 @@ import { estimatePasswordStrength } from '@douglasneuroinformatics/libpasswd';
44import { Form , Heading } from '@douglasneuroinformatics/libui/components' ;
55import { useTranslation } from '@douglasneuroinformatics/libui/hooks' ;
66import { $BasePermissionLevel , $CreateUserData } from '@opendatacapture/schemas/user' ;
7- import type { CreateUserData } from '@opendatacapture/schemas/user' ;
7+ import type { CreateUserData , User } from '@opendatacapture/schemas/user' ;
88import { createFileRoute , useNavigate } from '@tanstack/react-router' ;
99import { z } from 'zod/v4' ;
1010
1111import { PageHeader } from '@/components/PageHeader' ;
1212import { useCreateUserMutation } from '@/hooks/useCreateUserMutation' ;
1313import { groupsQueryOptions , useGroupsQuery } from '@/hooks/useGroupsQuery' ;
14+ import axios from 'axios' ;
1415
1516const 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