/
SignUpForm.tsx
243 lines (218 loc) · 7.41 KB
/
SignUpForm.tsx
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { useMemo, useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { useMutation } from '@tanstack/react-query';
import { useForm } from 'react-hook-form';
import { Center } from '@mantine/core';
import { showNotification } from '@mantine/notifications';
import { passwordConstraints, UTM_CAMPAIGN_QUERY_PARAM } from '@novu/shared';
import type { IResponseError } from '@novu/shared';
import { PasswordInput, Button, colors, Input, Text, Checkbox } from '@novu/design-system';
import { useAuthContext } from '../../../components/providers/AuthProvider';
import { api } from '../../../api/api.client';
import { applyToken, useVercelParams } from '../../../hooks';
import { useAcceptInvite } from './useAcceptInvite';
import { PasswordRequirementPopover } from './PasswordRequirementPopover';
import { buildGithubLink, buildVercelGithubLink } from './gitHubUtils';
import { ROUTES } from '../../../constants/routes.enum';
import { OAuth } from './OAuth';
type SignUpFormProps = {
invitationToken?: string;
email?: string;
};
export type SignUpFormInputType = {
email: string;
password: string;
fullName: string;
};
export function SignUpForm({ invitationToken, email }: SignUpFormProps) {
const navigate = useNavigate();
const { setToken } = useAuthContext();
const { isLoading: loadingAcceptInvite, submitToken } = useAcceptInvite();
const { isFromVercel, code, next, configurationId } = useVercelParams();
const vercelQueryParams = `code=${code}&next=${next}&configurationId=${configurationId}`;
const loginLink = isFromVercel ? `/auth/login?${vercelQueryParams}` : ROUTES.AUTH_LOGIN;
const githubLink = isFromVercel
? buildVercelGithubLink({ code, next, configurationId })
: buildGithubLink({ invitationToken });
const { isLoading, mutateAsync, isError, error } = useMutation<
{ token: string },
IResponseError,
{
firstName: string;
lastName: string;
email: string;
password: string;
}
>((data) => api.post('/v1/auth/register', data));
const onSubmit = async (data) => {
const itemData = {
firstName: data.fullName.split(' ')[0],
lastName: data.fullName.split(' ')[1],
email: data.email,
password: data.password,
};
if (!itemData.lastName) {
showNotification({
message: 'Please write your full name including last name',
color: 'red',
});
return;
}
const response = await mutateAsync(itemData);
/**
* We need to call the applyToken to avoid a race condition for accept invite
* To get the correct token when sending the request
*/
const token = (response as any).token;
applyToken(token);
if (invitationToken) {
submitToken(token, invitationToken, false, true);
} else {
setToken(token);
}
navigate(isFromVercel ? `/auth/application?${vercelQueryParams}` : ROUTES.AUTH_APPLICATION);
return true;
};
const {
register,
handleSubmit,
control,
formState: { errors },
} = useForm<SignUpFormInputType>({
defaultValues: {
email,
fullName: '',
password: '',
},
});
const [accepted, setAccepted] = useState<boolean>(false);
const serverErrorString = useMemo<string>(() => {
return Array.isArray(error?.message) ? error?.message[0] : error?.message;
}, [error]);
const emailServerError = useMemo<string>(() => {
if (serverErrorString === 'User already exists') return 'An account with this email already exists';
if (serverErrorString === 'email must be an email') return 'Please provide a valid email';
return '';
}, [serverErrorString]);
const accountCreationError = useMemo<string>(() => {
if (serverErrorString === 'Account creation is disabled')
return 'The creation of new accounts is currently disabled. Please contact your administrator.';
return '';
}, [serverErrorString]);
return (
<>
<OAuth />
<form noValidate name="login-form" onSubmit={handleSubmit(onSubmit)}>
<Input
error={errors.fullName?.message}
{...register('fullName', {
required: 'Please input full name',
})}
required
data-test-id="fullName"
label="Full Name"
placeholder="Your full name goes here"
mt={5}
/>
<Input
error={errors.email?.message || emailServerError}
disabled={!!email}
{...register('email', {
required: 'Please provide an email',
pattern: { value: /^\S+@\S+\.\S+$/, message: 'Please provide a valid email' },
})}
required
label="Email"
placeholder="Type your email..."
data-test-id="email"
mt={20}
/>
<PasswordRequirementPopover control={control}>
<PasswordInput
error={errors.password?.message}
mt={20}
{...register('password', {
required: 'Password, not your birthdate',
minLength: { value: passwordConstraints.minLength, message: 'Minimum 8 characters' },
maxLength: {
value: passwordConstraints.maxLength,
message: 'Maximum 64 characters',
},
pattern: {
value: passwordConstraints.pattern,
message:
// eslint-disable-next-line max-len
'The password must contain minimum 8 and maximum 64 characters, at least one uppercase letter, one lowercase letter, one number and one special character #?!@$%^&*()-',
},
})}
required
label="Password"
placeholder="Type your password..."
data-test-id="password"
/>
</PasswordRequirementPopover>
<Checkbox
onChange={(prev) => setAccepted(prev.target.checked)}
required
label={<Accept />}
data-test-id="accept-cb"
mt={20}
mb={20}
/>
{accountCreationError && (
<Text mt={20} size="lg" align="center" color={colors.error}>
{accountCreationError}
</Text>
)}
<Button
disabled={!accepted}
mt={20}
inherit
loading={isLoading || loadingAcceptInvite}
submit
data-test-id="submitButton"
>
Sign Up {invitationToken ? '& Accept Invite' : null}
</Button>
<Center mt={20}>
<Text mr={10} size="md" color={colors.B60}>
Already have an account?
</Text>
<Link to={loginLink}>
<Text gradient> Sign In</Text>
</Link>
</Center>
</form>
{isError && !emailServerError && !accountCreationError && (
<Text mt={20} size="lg" weight="bold" align="center" color={colors.error}>
{' '}
{error?.message}
</Text>
)}
</>
);
}
function Accept() {
return (
<>
<span>I accept the </span>
<a
style={{ textDecoration: 'underline' }}
href={`https://novu.co/terms${UTM_CAMPAIGN_QUERY_PARAM}`}
target="_blank"
rel="noopener noreferrer"
>
Terms and Conditions
</a>
<span> and have read the </span>
<a
style={{ textDecoration: 'underline' }}
href={`https://novu.co/privacy${UTM_CAMPAIGN_QUERY_PARAM}`}
target="_blank"
rel="noopener noreferrer"
>
Privacy Policy
</a>
</>
);
}