-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.jsx
More file actions
110 lines (98 loc) · 3.08 KB
/
signup.jsx
File metadata and controls
110 lines (98 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import "./signup.css";
const Signup = ({ onAuthSuccess }) => {
const [username, setUsername] = useState("");
const [email, setEmail] = useState(""); // Added email field
const [password, setPassword] = useState("");
const [role, setRole] = useState("buyer"); // Default role
const navigate = useNavigate();
const handleSignup = async (e) => {
e.preventDefault();
try {
const res = await fetch("http://localhost:3001/signup", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, email, password, role }), // Include email
});
const data = await res.json();
if (res.ok && data.token && data.user) {
onAuthSuccess(data.token, data.user);
navigate("/dashboard"); // Redirect after successful signup
} else {
alert(data.error || "Signup failed");
}
} catch (error) {
console.error("Signup request failed:", error);
alert("An error occurred. Please try again.");
}
};
return (
<div className="signup-container">
<h2>Sign Up</h2>
<form onSubmit={handleSignup}>
<label htmlFor="username">Username:</label>
<input
type="text"
id="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
/>
<label htmlFor="email">Email:</label> {/* Added email input */}
<input
type="email"
id="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
<label htmlFor="password">Password:</label>
<input
type="password"
id="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
<p>Select your role:</p>
<div className="role-options">
<label>
<input
type="radio"
name="role"
value="buyer"
checked={role === "buyer"}
onChange={() => setRole("buyer")}
/>
Buyer
</label>
<label>
<input
type="radio"
name="role"
value="seller"
checked={role === "seller"}
onChange={() => setRole("seller")}
/>
Seller
</label>
<label>
<input
type="radio"
name="role"
value="refurbisher"
checked={role === "refurbisher"}
onChange={() => setRole("refurbisher")}
/>
Refurbisher
</label>
</div>
<button type="submit">Sign Up</button>
</form>
<p>Already have an account?</p>
<button onClick={() => navigate("/login")}>Login Here</button>
</div>
);
};
export default Signup;