diff --git a/frontend/src/CUTT.tsx b/frontend/src/CUTT.tsx index 6999e5c..1dfce68 100644 --- a/frontend/src/CUTT.tsx +++ b/frontend/src/CUTT.tsx @@ -7,6 +7,8 @@ import Rankings from "./Form"; import TeamPanel from "./TeamPanel"; import { GraphComponent } from "./Network"; import MVPChart from "./MVPChart"; +import { SetPassword } from "./SetPassword"; +import { Register } from "./Register"; const Maintenance = () => { return ( @@ -24,6 +26,7 @@ function App() { return ( + } /> { + const [name, setName] = useState(""); + const [username, setUsername] = useState(""); + const [gender, setGender] = useState(); + const [number, setNumber] = useState(""); + const [email, setEmail] = useState(""); + const [teamName, setTeamName] = useState(""); + const [teamID, setTeamID] = useState(); + const [password, setPassword] = useState(""); + const [passwordr, setPasswordr] = useState(""); + const [passwordHint, setPasswordHint] = useState(""); + const [token, setToken] = useState(""); + const [error, setError] = useState(""); + const [loading, setLoading] = useState(false); + const navigate = useNavigate(); + + useEffect(() => { + const params = new URLSearchParams(window.location.search); + const token = params.get("token"); + if (token) { + setToken(token); + try { + const payload = jwtDecode(token); + if (payload.sub === "register") { + if (payload.team_id) setTeamID(payload.team_id); + } else { + setError("not a valid token for registration"); + } + if (payload.name) setTeamName(payload.name); + } catch (InvalidTokenError) { + setError("not a valid token"); + } + } else setError("no token found"); + }, []); + + function passwordCheck() { + if (password === passwordr) { + setPasswordHint(""); + } else setPasswordHint("passwords do not match"); + } + + async function handleSubmit(e: FormEvent) { + e.preventDefault(); + if (password === passwordr) { + const req = new Request(`${baseUrl}api/register`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + display_name: name, + username: username, + email: email, + number: number, + gender: gender, + team_id: teamID, + token: token, + password: password, + }), + }); + let resp: Response; + try { + resp = await fetch(req); + } catch (e) { + throw new Error(`request failed: ${e}`); + } + setLoading(false); + + if (resp.ok) { + console.log(resp); + navigate("/", { + replace: true, + state: { username: username, password: password }, + }); + } + + if (!resp.ok) { + const { detail } = await resp.json(); + if (detail) setError(detail); + else setError("unauthorized"); + throw new Error("Unauthorized"); + } + } else setError("the passwords did not match"); + } + + return ( +
+
+

+ Register {teamName && `in team "${teamName}"`} +

+
+
+
+ +
+ { + setName(e.target.value); + setError(""); + }} + /> +
+
+
+ +
+ { + setUsername(e.target.value); + setError(""); + }} + /> +
+
+
+ + { + setError(""); + setPassword(evt.target.value); + }} + /> +
+
+ + { + setError(""); + setPasswordr(evt.target.value); + passwordCheck(); + }} + /> +

{passwordHint}

+
+
+
+ +
+
+ +
+
+
+
+ +
+ { + setNumber(e.target.value); + setError(""); + }} + /> +
+
+
+ +
+ { + setEmail(e.target.value); + setError(""); + }} + /> +
+

+ {error} +

+
+
+
+ +
+
+
+
+ ); +};