forked from prashantsahu99/HackOctoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Signup.js
164 lines (146 loc) · 6.13 KB
/
Signup.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React, { useState } from "react";
import { useFormik } from "formik";
import * as yup from "yup";
import { Link } from "react-router-dom"
import { useNavigate } from "react-router-dom"
function Signup() {
const navigate = useNavigate();
const [flag, setFlag] = useState(false);
const [login, setLogin] = useState(true);
const [isAlreadyExist, setIsAlreadyExist] = useState(false);
const initialValues = {
email: "",
password: "",
name: "",
};
const onSubmit = (values) => {
};
const validationSchema = yup.object({
name: yup.string().required("Required!!"),
email: yup.string().email("Invalid email format").required("Required!!"),
password: yup
.string()
.required("Required!!")
.matches(
/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/,
"Must Contain 8 Characters, One Uppercase, One Lowercase, One Number and one special case Character"
),
});
const formik = useFormik({
initialValues,
onSubmit,
validationSchema,
});
function handleFormSubmit(e) {
formik.handleSubmit(e);
if (!formik.values.email || !formik.values.password || !formik.values.name) {
setFlag(true);
} else {
setFlag(false);
let ob = {
name: formik.values.name,
email: formik.values.email,
password: formik.values.password,
}
let olddata = localStorage.getItem('formdata');
if (olddata == null) {
olddata = []
olddata.push(ob)
localStorage.setItem('formdata', JSON.stringify(olddata));
} else {
let oldArr = JSON.parse(olddata)
if (
oldArr.some(
(user) =>
user.email === formik.values.email
)
) {
setIsAlreadyExist(!isAlreadyExist);
}
else {
oldArr.push(ob)
localStorage.setItem("formdata", JSON.stringify(oldArr))
console.log(oldArr, 'hhg')
navigate("/login")
}
}
}
console.log(isAlreadyExist);
}
return (
<div className="container">
<div className="row justify-content-center">
<div className="col-md-6 col-lg-6 " >
<div className="mb-5">
<p className="h2 text-center m-3">Sign Up </p>
<div className = "text-center">
<span className="h6 opacity-50">Already Registered?</span>
<Link to="/login">
<span>Login</span>
</Link>
</div>
</div>
<form onSubmit={handleFormSubmit} >
<div className="form-control bg-muted">
<label htmlFor="name" className="form-label fw-bold ">Name</label>
<input
type="string"
id="name"
name="name"
className="form-control bg-light mb-2"
placeholder="Enter Your Name"
onBlur={formik.handleBlur}
onChange={formik.handleChange}
value={formik.values.name}
></input>
<div className="error text-danger mb-2">
{formik.touched.name && formik.errors.name ? (
<div>{formik.errors.name}</div>
) : null}
</div>
<label htmlFor="email" className="form-label fw-bold ">Email</label>
<input
type="email"
id="email"
name="email"
className="form-control bg-light mb-2"
placeholder="Enter Your Email"
onBlur={formik.handleBlur}
onChange={formik.handleChange}
value={formik.values.email}
></input>
<div className="error text-danger mb-2">
{formik.touched.email && formik.errors.email ? (
<div>{formik.errors.email}</div>
) : null}
</div>
<label htmlFor="password" className="form-label fw-bold ">Password</label>
<input
type="password"
id="password"
name="password"
className="form-control mb-2 bg-light "
placeholder="Enter Password "
onBlur={formik.handleBlur}
onChange={formik.handleChange}
value={formik.values.password}
></input>
<div className="form-error text-danger mt-0">
{formik.touched.password && formik.errors.password ? (
<div>{formik.errors.password}</div>
) : null}
</div>
<div className=" mb-4 mt-4">
<div className="text-center">
<button type="submit" className="btn btn-primary btn-lg col-12">Signup</button>
</div>
</div>
{isAlreadyExist ? <div className="text-danger text-center">User Already Exists! </div> : null}
</div>
</form>
</div>
</div>
</div>
);
}
export default Signup;