From 7693cd13677772e83403c3c2f60e3419f498812e Mon Sep 17 00:00:00 2001 From: adityapat24 Date: Tue, 21 Oct 2025 15:38:01 -0400 Subject: [PATCH 1/2] register changes --- frontend/src/Register.tsx | 42 +++++--------------- frontend/src/context/auth/authContext.tsx | 47 +++++++++++++++-------- 2 files changed, 41 insertions(+), 48 deletions(-) diff --git a/frontend/src/Register.tsx b/frontend/src/Register.tsx index 402a6cc..e892087 100644 --- a/frontend/src/Register.tsx +++ b/frontend/src/Register.tsx @@ -4,6 +4,7 @@ import { observer } from "mobx-react-lite"; import { useNavigate } from "react-router-dom"; import logo from "./images/bcan_logo.svg"; import { api } from "./api"; +import { useAuthContext } from "./context/auth/authContext"; /** * Register a new BCAN user @@ -14,41 +15,15 @@ const Register = observer(() => { const [password, setPassword] = useState(""); const navigate = useNavigate(); + const { register } = useAuthContext(); + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); - - try { - const response = await api("/auth/register", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ username, password, email }), - }); - - const data = await response.json(); - - if (!response.ok) { - alert(data.message || "Registration failed."); - return; - } - - // If registration succeeded, automatically log in the user - const loginResponse = await api("/auth/login", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ username, password }), - }); - - const loginData = await loginResponse.json(); - - if (loginResponse.ok && loginData.access_token) { - setAuthState(true, loginData.user, loginData.access_token); - navigate("/grant-info"); - } else { - alert(loginData.message || "Login after registration failed."); - } - } catch (error) { - console.error("Error during registration:", error); - alert("An error occurred while registering. Please try again later."); + const success = await register(username, password, email); + if (success) { + navigate("/login"); + } else { + console.warn("Registration failed"); } }; @@ -80,6 +55,7 @@ const Register = observer(() => { > ← Back to Sign In + {/* Username field */}