-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignUp.js
196 lines (191 loc) · 6.63 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import * as React from "react";
import {
Button,
Flex,
FormControl,
FormErrorMessage,
FormLabel,
Input,
InputGroup,
InputRightElement,
} from "@chakra-ui/react";
import { useFormik } from "formik";
import * as Yup from "yup";
import { useRouter } from "next/router";
import { useToast } from "@chakra-ui/react";
import { supabaseClient } from "../lib/client";
import { useState } from "react";
export default function SignUp() {
const router = useRouter();
const [show, setShow] = React.useState(false);
const [error, setError] = useState(null);
const handleClick = () => setShow(!show);
const [isLoading, setIsLoading] = useState(false);
const [isSubmitted, setIsSubmitted] = useState(false);
const toast = useToast();
const toastMsg = (desc) => {
return toast({
title: "Failed",
description: desc,
status: "error",
duration: 1000,
isClosable: true,
});
};
const {
touched,
errors,
getFieldProps,
validateForm,
isValid,
dirty,
isSubmitting,
handleSubmit,
handleChange,
handleBlur,
values,
} = useFormik({
enableReinitialize: true,
initialValues: {
name: "",
email: "",
password: "",
},
validationSchema: Yup.object().shape({
name: Yup.string().required("Required"),
email: Yup.string().email("Invalid email").required("Required"),
password: Yup.string()
.matches(
/^(?=.*[A-Z])(?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z]).{8,}$/,
"Password must contain at least 8 characters, one uppercase, one lowercase and one number"
)
.required("Required"),
}),
async onSubmit(values, formikActions) {
try {
const { error } = await supabaseClient.auth.signUp(
{
email: values.email,
password: values.password,
},
{
data: {
first_name: values.name,
},
}
);
if (error) {
toastMsg(error.message);
console.log(error);
} else {
setIsSubmitted(true);
}
} catch (error) {
console.log(error);
setError(error.message);
} finally {
setIsLoading(false);
}
},
});
const isDisabled = () => {
return !(isValid && dirty) || isSubmitting;
};
return (
<div className="flex flex-col flex-1 justify-between">
<div>
<FormControl
isInvalid={Boolean(errors.name)}
isRequired
className="my-4"
size="md"
>
<FormLabel htmlFor="email">First Name</FormLabel>
<InputGroup size="md">
<Input
height={50}
pr="4.5rem"
type={"name"}
errorBorderColor="red.300"
placeholder="First Name"
onChange={handleChange("name")}
autoComplete={"name"}
value={values.name}
isInvalid={Boolean(errors.name)}
/>
</InputGroup>
<FormErrorMessage>{errors.name}</FormErrorMessage>
</FormControl>
<FormControl
isInvalid={Boolean(errors.email)}
isRequired
className="my-4"
size="md"
>
<FormLabel htmlFor="email">Email address</FormLabel>
<InputGroup size="md">
<Input
height={50}
pr="4.5rem"
type={"email"}
errorBorderColor="red.300"
placeholder="Email"
onChange={handleChange("email")}
autoComplete={"email"}
value={values.email}
isInvalid={Boolean(errors.email)}
/>
</InputGroup>
<FormErrorMessage>{errors.email}</FormErrorMessage>
</FormControl>
<FormControl
isInvalid={Boolean(errors.password)}
isRequired
className="my-4"
>
<FormLabel htmlFor="password">Password</FormLabel>
<InputGroup size="md">
<Input
height={50}
pr="4.5rem"
errorBorderColor="red.300"
type={show ? "text" : "password"}
placeholder="Password"
onChange={handleChange("password")}
value={values.password}
isInvalid={Boolean(errors.password)}
/>
<InputRightElement height={50} width="4.5rem">
<Button h="1.75rem" size="sm" onClick={handleClick}>
{show ? "Hide" : "Show"}
</Button>
</InputRightElement>
</InputGroup>
<FormErrorMessage>{errors.password}</FormErrorMessage>
</FormControl>
</div>
<div
className="flex flex-col flex-1 mt-12"
style={{
paddingBottom: 120,
}}
>
<Button
type="submit"
variant="contained"
sx={{ mt: 3, mb: 2 }}
style={{
backgroundColor: "black",
borderRadius: 8,
color: "white",
height: 60,
}}
onClick={handleSubmit}
isDisabled={isDisabled()}
>
Sign Up
</Button>
</div>
</div>
);
}