2023-04-26 15:14:28 +02:00
|
|
|
"use client";
|
|
|
|
|
|
2023-05-16 17:34:52 +02:00
|
|
|
import {
|
|
|
|
|
LegalAndSupportSettings,
|
|
|
|
|
PasswordComplexitySettings,
|
|
|
|
|
} from "@zitadel/server";
|
2023-04-26 15:14:28 +02:00
|
|
|
import PasswordComplexity from "./PasswordComplexity";
|
2023-04-26 18:36:09 +02:00
|
|
|
import { useState } from "react";
|
2023-04-26 15:14:28 +02:00
|
|
|
import { Button, ButtonVariants } from "./Button";
|
|
|
|
|
import { TextInput } from "./Input";
|
|
|
|
|
import { PrivacyPolicyCheckboxes } from "./PrivacyPolicyCheckboxes";
|
2023-04-26 16:04:56 +02:00
|
|
|
import { FieldValues, useForm } from "react-hook-form";
|
|
|
|
|
import {
|
|
|
|
|
lowerCaseValidator,
|
|
|
|
|
numberValidator,
|
|
|
|
|
symbolValidator,
|
|
|
|
|
upperCaseValidator,
|
|
|
|
|
} from "#/utils/validators";
|
2023-04-27 17:07:57 +02:00
|
|
|
import { useRouter } from "next/navigation";
|
2023-04-27 17:43:15 +02:00
|
|
|
import { Spinner } from "./Spinner";
|
2023-04-26 16:04:56 +02:00
|
|
|
|
|
|
|
|
type Inputs =
|
|
|
|
|
| {
|
|
|
|
|
firstname: string;
|
|
|
|
|
lastname: string;
|
|
|
|
|
email: string;
|
|
|
|
|
password: string;
|
|
|
|
|
confirmPassword: string;
|
|
|
|
|
}
|
|
|
|
|
| FieldValues;
|
2023-04-26 15:14:28 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2023-05-17 17:04:56 +02:00
|
|
|
legal: LegalAndSupportSettings;
|
2023-05-22 13:02:39 +02:00
|
|
|
passwordComplexitySettings: PasswordComplexitySettings;
|
2023-04-26 15:14:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function RegisterForm({
|
2023-05-17 17:04:56 +02:00
|
|
|
legal,
|
2023-05-22 13:02:39 +02:00
|
|
|
passwordComplexitySettings,
|
2023-04-26 15:14:28 +02:00
|
|
|
}: Props) {
|
2023-04-26 16:04:56 +02:00
|
|
|
const { register, handleSubmit, watch, formState } = useForm<Inputs>({
|
|
|
|
|
mode: "onBlur",
|
|
|
|
|
});
|
|
|
|
|
|
2023-04-27 17:43:15 +02:00
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
|
|
|
|
2023-04-27 17:07:57 +02:00
|
|
|
const router = useRouter();
|
|
|
|
|
|
2023-04-26 18:36:09 +02:00
|
|
|
async function submitRegister(values: Inputs) {
|
2023-04-27 17:43:15 +02:00
|
|
|
setLoading(true);
|
2023-06-30 14:13:03 +02:00
|
|
|
const res = await fetch("/api/registeruser", {
|
2023-04-26 18:36:09 +02:00
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
email: values.email,
|
|
|
|
|
password: values.password,
|
|
|
|
|
firstName: values.firstname,
|
|
|
|
|
lastName: values.lastname,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!res.ok) {
|
2023-04-27 17:43:15 +02:00
|
|
|
setLoading(false);
|
2023-04-26 18:36:09 +02:00
|
|
|
throw new Error("Failed to register user");
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-27 17:43:15 +02:00
|
|
|
setLoading(false);
|
2023-04-26 18:36:09 +02:00
|
|
|
return res.json();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-27 17:07:57 +02:00
|
|
|
function submitAndLink(value: Inputs): Promise<boolean | void> {
|
|
|
|
|
return submitRegister(value).then((resp: any) => {
|
2023-05-19 10:13:05 +02:00
|
|
|
return router.push(`/verify?userID=${resp.userId}`);
|
2023-04-27 17:07:57 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 16:04:56 +02:00
|
|
|
const { errors } = formState;
|
|
|
|
|
|
|
|
|
|
const watchPassword = watch("password", "");
|
|
|
|
|
const watchConfirmPassword = watch("confirmPassword", "");
|
|
|
|
|
|
2023-04-26 15:14:28 +02:00
|
|
|
const [tosAndPolicyAccepted, setTosAndPolicyAccepted] = useState(false);
|
|
|
|
|
|
2023-04-26 16:04:56 +02:00
|
|
|
const hasMinLength =
|
2023-05-22 13:02:39 +02:00
|
|
|
passwordComplexitySettings &&
|
|
|
|
|
watchPassword?.length >= passwordComplexitySettings.minLength;
|
2023-04-26 16:04:56 +02:00
|
|
|
const hasSymbol = symbolValidator(watchPassword);
|
|
|
|
|
const hasNumber = numberValidator(watchPassword);
|
|
|
|
|
const hasUppercase = upperCaseValidator(watchPassword);
|
|
|
|
|
const hasLowercase = lowerCaseValidator(watchPassword);
|
|
|
|
|
|
|
|
|
|
const policyIsValid =
|
2023-05-22 13:02:39 +02:00
|
|
|
passwordComplexitySettings &&
|
|
|
|
|
(passwordComplexitySettings.requiresLowercase ? hasLowercase : true) &&
|
|
|
|
|
(passwordComplexitySettings.requiresNumber ? hasNumber : true) &&
|
|
|
|
|
(passwordComplexitySettings.requiresUppercase ? hasUppercase : true) &&
|
|
|
|
|
(passwordComplexitySettings.requiresSymbol ? hasSymbol : true) &&
|
2023-04-26 16:04:56 +02:00
|
|
|
hasMinLength;
|
|
|
|
|
|
2023-04-26 15:14:28 +02:00
|
|
|
return (
|
|
|
|
|
<form className="w-full">
|
|
|
|
|
<div className="grid grid-cols-2 gap-4 mb-4">
|
|
|
|
|
<div className="">
|
2023-04-26 16:04:56 +02:00
|
|
|
<TextInput
|
|
|
|
|
type="firstname"
|
|
|
|
|
autoComplete="firstname"
|
|
|
|
|
required
|
|
|
|
|
{...register("firstname", { required: "This field is required" })}
|
|
|
|
|
label="First name"
|
|
|
|
|
error={errors.firstname?.message as string}
|
|
|
|
|
/>
|
2023-04-26 15:14:28 +02:00
|
|
|
</div>
|
|
|
|
|
<div className="">
|
2023-04-26 16:04:56 +02:00
|
|
|
<TextInput
|
|
|
|
|
type="lastname"
|
|
|
|
|
autoComplete="lastname"
|
|
|
|
|
required
|
|
|
|
|
{...register("lastname", { required: "This field is required" })}
|
|
|
|
|
label="Last name"
|
|
|
|
|
error={errors.lastname?.message as string}
|
|
|
|
|
/>
|
2023-04-26 15:14:28 +02:00
|
|
|
</div>
|
|
|
|
|
<div className="col-span-2">
|
2023-04-26 16:04:56 +02:00
|
|
|
<TextInput
|
|
|
|
|
type="email"
|
|
|
|
|
autoComplete="email"
|
|
|
|
|
required
|
|
|
|
|
{...register("email", { required: "This field is required" })}
|
2023-04-26 18:36:09 +02:00
|
|
|
label="E-mail"
|
2023-04-26 16:04:56 +02:00
|
|
|
error={errors.email?.message as string}
|
|
|
|
|
/>
|
2023-04-26 15:14:28 +02:00
|
|
|
</div>
|
|
|
|
|
<div className="">
|
2023-04-26 16:04:56 +02:00
|
|
|
<TextInput
|
|
|
|
|
type="password"
|
|
|
|
|
autoComplete="new-password"
|
|
|
|
|
required
|
|
|
|
|
{...register("password", {
|
|
|
|
|
required: "You have to provide a password!",
|
|
|
|
|
})}
|
|
|
|
|
label="Password"
|
|
|
|
|
error={errors.password?.message as string}
|
|
|
|
|
/>
|
2023-04-26 15:14:28 +02:00
|
|
|
</div>
|
|
|
|
|
<div className="">
|
2023-04-26 16:04:56 +02:00
|
|
|
<TextInput
|
|
|
|
|
type="password"
|
|
|
|
|
required
|
|
|
|
|
autoComplete="new-password"
|
|
|
|
|
{...register("confirmPassword", {
|
|
|
|
|
required: "This field is required",
|
|
|
|
|
})}
|
|
|
|
|
label="Confirm Password"
|
|
|
|
|
error={errors.confirmPassword?.message as string}
|
|
|
|
|
/>
|
2023-04-26 15:14:28 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2023-05-22 13:02:39 +02:00
|
|
|
{passwordComplexitySettings && (
|
2023-04-26 15:14:28 +02:00
|
|
|
<PasswordComplexity
|
2023-05-22 13:02:39 +02:00
|
|
|
passwordComplexitySettings={passwordComplexitySettings}
|
2023-04-26 16:04:56 +02:00
|
|
|
password={watchPassword}
|
|
|
|
|
equals={!!watchPassword && watchPassword === watchConfirmPassword}
|
2023-04-26 15:14:28 +02:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
2023-05-17 17:04:56 +02:00
|
|
|
{legal && (
|
2023-04-26 15:14:28 +02:00
|
|
|
<PrivacyPolicyCheckboxes
|
2023-05-17 17:04:56 +02:00
|
|
|
legal={legal}
|
2023-04-26 15:14:28 +02:00
|
|
|
onChange={setTosAndPolicyAccepted}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div className="mt-8 flex w-full flex-row items-center justify-between">
|
|
|
|
|
<Button type="button" variant={ButtonVariants.Secondary}>
|
|
|
|
|
back
|
|
|
|
|
</Button>
|
2023-04-26 16:04:56 +02:00
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
variant={ButtonVariants.Primary}
|
|
|
|
|
disabled={
|
2023-04-27 17:43:15 +02:00
|
|
|
loading ||
|
2023-04-26 16:04:56 +02:00
|
|
|
!policyIsValid ||
|
|
|
|
|
!formState.isValid ||
|
|
|
|
|
!tosAndPolicyAccepted ||
|
|
|
|
|
watchPassword !== watchConfirmPassword
|
|
|
|
|
}
|
2023-04-27 17:07:57 +02:00
|
|
|
onClick={handleSubmit(submitAndLink)}
|
2023-04-26 16:04:56 +02:00
|
|
|
>
|
2023-04-27 17:43:15 +02:00
|
|
|
{loading && <Spinner className="h-5 w-5 mr-2" />}
|
2023-04-26 15:14:28 +02:00
|
|
|
continue
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|