import React, { useState } from "react"; import { Link, useNavigate } from "react-router-dom"; import illustration from "../../assets/illustration_img.png"; import styles from "./Signup.module.css"; export default function Signup() { const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [message, setMessage] = useState(""); const [messageType, setMessageType] = useState(""); // New state to manage message type const navigate = useNavigate(); const handleSubmit = async (e) => { e.preventDefault(); const payload = { username: name, email: email, password: password, }; try { const response = await fetch("http://localhost:8080/api/auth/register", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(payload), }); if (response.ok) { setMessageType("success"); setMessage("User registered successfully!"); // Wait 3 seconds and then redirect to login page setTimeout(() => { navigate("/login"); }, 3000); } else if (response.status === 409) { setMessageType("error"); setMessage("Email is already taken."); } else { setMessageType("error"); setMessage("Registration failed."); } } catch (error) { console.error("Error:", error); setMessageType("error"); setMessage("Error registering user."); } }; return (
illustration
CREATE AN ACCOUNT
setName(e.target.value)} placeholder="Enter your username" required />
setEmail(e.target.value)} placeholder="Enter your email" required />
setPassword(e.target.value)} placeholder="Enter your password" required />
{/* Display message with appropriate styling */} {message && (

{message}

)}

OR

Have an account? Login

); }