diff --git a/client/src/controllers/UserController.ts b/client/src/controllers/UserController.ts index e7ab427..48b0df3 100644 --- a/client/src/controllers/UserController.ts +++ b/client/src/controllers/UserController.ts @@ -29,10 +29,22 @@ const login = async ({email, password}: {email: string, password: string}) => { return data; }; -const signup = async (email: string, password: string) => { +const signup = async (formData: FormData) => { + const form = { + email: formData.get('email') as string, + password: formData.get('password') as string, + "password-confirm": formData.get('password-confirm') as string, + firstName: formData.get('firstName') as string, + lastName: formData.get('lastName') as string, + }; + + if (form.password !== form["password-confirm"]) { + throw "Passwords do not match"; + } + const response = await fetch('/api/auth/signup', { method: 'POST', - body: JSON.stringify({ email, password }), + body: JSON.stringify(form), mode: 'cors', headers: { 'Content-Type': 'application/json', diff --git a/client/src/routes/signup.tsx b/client/src/routes/signup.tsx index b8367ae..b89d7e7 100644 --- a/client/src/routes/signup.tsx +++ b/client/src/routes/signup.tsx @@ -1,6 +1,22 @@ +import { Token } from '@/types'; import logo from '@assets/images/logo.svg'; +import { toastError, toastSuccess } from '@controllers/Toasts'; +import { signup } from '@controllers/UserController'; +import { useNavigate } from 'react-router-dom'; export default () => { + const navigate = useNavigate(); + const onSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + const data = new FormData(e.target as HTMLFormElement); + signup(data).then((data: Token) => { + toastSuccess('You have successfully signed up!'); + navigate('/login'); + }).catch((err) => { + toastError(err as string); + }); + }; + return ( <>
@@ -8,31 +24,31 @@ export default () => { Groupomania
-
+ onSubmit(e)}>
-
-