-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRegister.js
More file actions
142 lines (132 loc) · 3.92 KB
/
Register.js
File metadata and controls
142 lines (132 loc) · 3.92 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { useState } from "react";
import { useNavigate, Link } from "react-router-dom";
import "../assets/styles/LoginRegister.css";
/**
* Amanda Au-Yeung
* registers user with the email and pw to passport, session
* @returns jsx of registration form
*/
function Register() {
const [user, setUser] = useState({
email: "",
password: "",
confirmedPassword: "",
});
const [error, setError] = useState({
email: "",
password: "",
confirmedPassword: "",
});
const navigate = useNavigate();
const createUser = async (e) => {
e.preventDefault();
const res = await fetch("/api/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: user.email,
password: user.password,
}),
});
handleRegister();
const resRegUser = await res.json();
alert(resRegUser.message);
};
const onInputChange = (evt) => {
const { value, name } = evt.target;
setUser((prev) => ({
...prev,
[name]: value,
}));
validateInput(evt);
};
// This is a very good design that you validated the user's password, this is a good practice and our group should add this function too.
const validateInput = (evt) => {
let { name, value } = evt.target;
setError((prev) => {
const obj = { ...prev, [name]: "" };
if (user.password && value !== user.password) {
obj[name] = "The confirmed password does not match with the password";
}
return obj;
});
};
const handleRegister = () => {
navigate("/login", { replace: true });
};
// The align of the components still needs more adjustment
return (
<main className="card" id="signupCard">
<div className="alternate-text">
<Link id="sign-in" to="/login">
<div>Already have an account?</div>
<div>Sign In</div>
</Link>
</div>
<div className="signUp-title">
<h1 className="card-title" id="signUp">
Sign Up
</h1>
</div>
<div className="log-reg-body">
<form className="form-body" onSubmit={createUser}>
<div className="mb-3">
<label htmlFor="exampleInputEmail1" className="form-label">
Email address
</label>
<input
type="email"
className="form-control"
id="exampleInputEmail1"
placeholder="Enter Email"
value={user.email}
onChange={onInputChange}
name="email"
required
/>
<div id="emailHelp" className="form-text">
We will never share your email with anyone else.
</div>
</div>
<div className="mb-3">
<label htmlFor="inputPassword5">Password</label>
<input
type="password"
id="inputPassword5"
className="form-control"
placeholder="Enter your password"
name="password"
value={user.password}
onChange={onInputChange}
required
/>
</div>
<div className="mb-3">
<div htmlFor="inputPassword5">Confirm Password</div>
<input
type="password"
id="inputConfirmedPassword5"
className="form-control"
placeholder="Confirm your password"
name="confirmedPassword"
value={user.confirmedPassword}
onChange={onInputChange}
onBlur={validateInput}
required
/>
{error.confirmedPassword && (
<span className="err">{error.confirmedPassword}</span>
)}
</div>
<button type="submit" className="btn">
Sign Up
</button>
</form>
</div>
</main>
);
}
Register.protTypes = {};
export default Register;