From 691aa744a0398f185b3ca98a36fbd83806c7786c Mon Sep 17 00:00:00 2001 From: Parker Date: Sun, 10 Nov 2024 16:36:16 -0600 Subject: TOO MUCH STUFF --- app/src/components/Signup.tsx | 106 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 app/src/components/Signup.tsx (limited to 'app/src/components/Signup.tsx') diff --git a/app/src/components/Signup.tsx b/app/src/components/Signup.tsx new file mode 100644 index 0000000..293b51a --- /dev/null +++ b/app/src/components/Signup.tsx @@ -0,0 +1,106 @@ +import { useState, FormEvent } from 'react'; +import styles from '../styles/Login.module.css'; +import { Link } from 'react-router-dom'; +import { useNavigate } from 'react-router-dom'; +import axios from 'axios'; + +function Signup() { + const [username, setUsername] = useState(''); + const [password, setPassword] = useState(''); + const [passwordConfirm, setPasswordConfirm] = useState(''); + const [isSubmitting, setIsSubmitting] = useState(false); + const [error, setError] = useState(null); + + const navigate = useNavigate(); + + const handleSubmit = async (e: FormEvent) => { + e.preventDefault(); + setIsSubmitting(true); + + if (password !== passwordConfirm) { + setPassword(''); + setPasswordConfirm(''); + return setError('Passwords do not match.'); + } + + try { + const res = await axios.post( + '/api/users/register', + new URLSearchParams({ + username: username, + password: password, + }), + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + } + ); + + if (res.status === 200) { + navigate('/login'); + } + } catch (error: unknown) { + if (axios.isAxiosError(error)) { + const customErrorMessage = error.response?.data?.detail || null; + setUsername(''); + setPassword(''); + setPasswordConfirm(''); + setError(customErrorMessage || 'An error occurred. Please try again.'); + } else { + setUsername(''); + setPassword(''); + setPasswordConfirm(''); + setError('Unknown error. Please try again.'); + } + } finally { + setIsSubmitting(false); + } + }; + + return ( +
+

Sign up

+

+ {error} +

+
+
+
+
+ setUsername(e.target.value)} + required + /> + setPassword(e.target.value)} + required + /> + setPasswordConfirm(e.target.value)} + required + /> + +
+
+

Already have an account? Log in here.

+
+
+
+ ); +} + +export default Signup; \ No newline at end of file -- cgit v1.2.3-70-g09d2