[d565449] | 1 | import React, { useState } from "react";
|
---|
| 2 | import { Link, useNavigate } from "react-router-dom";
|
---|
| 3 | import illustration from "../../assets/illustration_img.png";
|
---|
| 4 | import styles from "./Signup.module.css";
|
---|
[0c6b92a] | 5 | import Logo from "../../components/Logo/Logo";
|
---|
[d565449] | 6 |
|
---|
| 7 | export default function Signup() {
|
---|
| 8 | const [name, setName] = useState("");
|
---|
| 9 | const [email, setEmail] = useState("");
|
---|
| 10 | const [password, setPassword] = useState("");
|
---|
| 11 | const [message, setMessage] = useState("");
|
---|
| 12 | const [messageType, setMessageType] = useState(""); // New state to manage message type
|
---|
| 13 | const navigate = useNavigate();
|
---|
| 14 |
|
---|
| 15 | const handleSubmit = async (e) => {
|
---|
| 16 | e.preventDefault();
|
---|
| 17 |
|
---|
| 18 | const payload = {
|
---|
| 19 | username: name,
|
---|
| 20 | email: email,
|
---|
| 21 | password: password,
|
---|
| 22 | };
|
---|
| 23 |
|
---|
| 24 | try {
|
---|
| 25 | const response = await fetch("http://localhost:8080/api/auth/register", {
|
---|
| 26 | method: "POST",
|
---|
| 27 | headers: {
|
---|
| 28 | "Content-Type": "application/json",
|
---|
| 29 | },
|
---|
| 30 | body: JSON.stringify(payload),
|
---|
| 31 | });
|
---|
| 32 |
|
---|
| 33 | if (response.ok) {
|
---|
| 34 | setMessageType("success");
|
---|
| 35 | setMessage("User registered successfully!");
|
---|
| 36 |
|
---|
| 37 | // Wait 3 seconds and then redirect to login page
|
---|
| 38 | setTimeout(() => {
|
---|
| 39 | navigate("/login");
|
---|
| 40 | }, 3000);
|
---|
| 41 | } else if (response.status === 409) {
|
---|
| 42 | setMessageType("error");
|
---|
| 43 | setMessage("Email is already taken.");
|
---|
| 44 | } else {
|
---|
| 45 | setMessageType("error");
|
---|
| 46 | setMessage("Registration failed.");
|
---|
| 47 | }
|
---|
| 48 | } catch (error) {
|
---|
| 49 | console.error("Error:", error);
|
---|
| 50 | setMessageType("error");
|
---|
| 51 | setMessage("Error registering user.");
|
---|
| 52 | }
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | return (
|
---|
| 56 | <div className={styles.wrapper}>
|
---|
[0c6b92a] | 57 | <Logo></Logo>
|
---|
[d565449] | 58 | <div className={styles.illustration}>
|
---|
| 59 | <img src={illustration} alt="illustration" />
|
---|
| 60 | </div>
|
---|
| 61 | <div className={styles.form}>
|
---|
| 62 | <div className={styles.heading}>CREATE AN ACCOUNT</div>
|
---|
| 63 | <form onSubmit={handleSubmit}>
|
---|
| 64 | <div>
|
---|
| 65 | <label htmlFor="name">Username</label>
|
---|
| 66 | <input
|
---|
| 67 | type="text"
|
---|
| 68 | id="name"
|
---|
| 69 | value={name}
|
---|
| 70 | onChange={(e) => setName(e.target.value)}
|
---|
| 71 | placeholder="Enter your username"
|
---|
| 72 | required
|
---|
| 73 | />
|
---|
| 74 | </div>
|
---|
| 75 | <div>
|
---|
| 76 | <label htmlFor="email">E-Mail</label>
|
---|
| 77 | <input
|
---|
| 78 | type="email"
|
---|
| 79 | id="email"
|
---|
| 80 | value={email}
|
---|
| 81 | onChange={(e) => setEmail(e.target.value)}
|
---|
| 82 | placeholder="Enter your email"
|
---|
| 83 | required
|
---|
| 84 | />
|
---|
| 85 | </div>
|
---|
| 86 | <div>
|
---|
| 87 | <label htmlFor="password">Password</label>
|
---|
| 88 | <input
|
---|
| 89 | type="password"
|
---|
| 90 | id="password"
|
---|
| 91 | value={password}
|
---|
| 92 | onChange={(e) => setPassword(e.target.value)}
|
---|
| 93 | placeholder="Enter your password"
|
---|
| 94 | required
|
---|
| 95 | />
|
---|
| 96 | </div>
|
---|
| 97 | <button type="submit">Submit</button>
|
---|
| 98 |
|
---|
| 99 | {/* Display message with appropriate styling */}
|
---|
| 100 | {message && (
|
---|
| 101 | <p className={messageType === "success" ? styles.successMessage : styles.errorMessage}>
|
---|
| 102 | {message}
|
---|
| 103 | </p>
|
---|
| 104 | )}
|
---|
| 105 | <h2 align="center" className={styles.or}>
|
---|
| 106 | OR
|
---|
| 107 | </h2>
|
---|
| 108 | </form>
|
---|
| 109 | <p>
|
---|
| 110 | Have an account? <Link to="/Login"> Login </Link>
|
---|
| 111 | </p>
|
---|
| 112 | </div>
|
---|
| 113 | </div>
|
---|
| 114 | );
|
---|
| 115 | }
|
---|