11"use client" ;
22import { DashboardSidebar } from "@/components" ;
33import { isValidEmailAddressFormat } from "@/lib/utils" ;
4- import React , { useEffect , useState } from "react" ;
4+ import React , { useState } from "react" ;
55import toast from "react-hot-toast" ;
6+ import { sanitizeFormData } from "@/lib/form-sanitize" ;
67
78const DashboardCreateNewUser = ( ) => {
8- const [ userInput , setUserInput ] = useState ( {
9+ const [ userInput , setUserInput ] = useState < {
10+ email : string ;
11+ password : string ;
12+ role : string ;
13+ } > ( {
914 email : "" ,
1015 password : "" ,
1116 role : "user" ,
1217 } ) ;
1318
14- const addNewUser = ( ) => {
19+ const addNewUser = async ( ) => {
20+ if ( userInput . email === "" || userInput . password === "" ) {
21+ toast . error ( "You must enter all input values to add a user" ) ;
22+ return ;
23+ }
24+
25+ // Sanitize form data before sending to API
26+ const sanitizedUserInput = sanitizeFormData ( userInput ) ;
27+
1528 if (
1629 userInput . email . length > 3 &&
1730 userInput . role . length > 0 &&
@@ -26,7 +39,7 @@ const DashboardCreateNewUser = () => {
2639 const requestOptions : any = {
2740 method : "post" ,
2841 headers : { "Content-Type" : "application/json" } ,
29- body : JSON . stringify ( userInput ) ,
42+ body : JSON . stringify ( sanitizedUserInput ) ,
3043 } ;
3144 ap ( `/api/users` , requestOptions )
3245 . then ( ( response ) => {
0 commit comments