Files
zitadel/apps/login/src/components/verify-email-form.tsx

179 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-05-17 17:04:56 +02:00
"use client";
2024-09-26 22:50:55 -04:00
import { Alert } from "@/components/alert";
2024-10-21 10:54:53 +02:00
import { resendVerifyEmail, verifyUser } from "@/lib/server/email";
2024-09-11 16:49:26 +02:00
import { LoginSettings } from "@zitadel/proto/zitadel/settings/v2/login_settings_pb";
2024-10-21 10:54:53 +02:00
import { AuthenticationMethodType } from "@zitadel/proto/zitadel/user/v2/user_service_pb";
2024-10-10 16:15:10 +02:00
import { useTranslations } from "next-intl";
2024-09-05 13:48:33 +02:00
import { useRouter } from "next/navigation";
2023-05-22 11:48:18 +02:00
import { useEffect, useState } from "react";
2024-09-05 13:48:33 +02:00
import { useForm } from "react-hook-form";
2024-10-21 10:54:53 +02:00
import { PASSKEYS, PASSWORD } from "./auth-methods";
2024-09-26 22:50:55 -04:00
import { Button, ButtonVariants } from "./button";
import { TextInput } from "./input";
import { Spinner } from "./spinner";
2023-05-17 17:04:56 +02:00
type Inputs = {
code: string;
};
type Props = {
userId: string;
2024-09-11 16:49:26 +02:00
loginName: string;
2023-05-22 11:48:18 +02:00
code: string;
organization?: string;
2024-04-01 14:27:15 +02:00
authRequestId?: string;
sessionId?: string;
2024-09-11 16:49:26 +02:00
loginSettings?: LoginSettings;
2024-10-21 10:54:53 +02:00
isInvite: boolean;
2023-05-17 17:04:56 +02:00
};
2024-09-26 22:50:55 -04:00
export function VerifyEmailForm({
userId,
2024-09-11 16:49:26 +02:00
loginName,
code,
organization,
2024-04-01 14:27:15 +02:00
authRequestId,
sessionId,
2024-09-11 16:49:26 +02:00
loginSettings,
2024-10-21 10:54:53 +02:00
isInvite,
}: Props) {
2024-10-10 16:15:10 +02:00
const t = useTranslations("verify");
2023-05-17 17:04:56 +02:00
const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur",
2023-05-22 11:48:18 +02:00
defaultValues: {
code: code ?? "",
},
2023-05-17 17:04:56 +02:00
});
2024-10-21 10:54:53 +02:00
const [authMethods, setAuthMethods] = useState<
AuthenticationMethodType[] | null
>(null);
2023-05-22 11:48:18 +02:00
useEffect(() => {
2024-10-21 10:54:53 +02:00
if (code && userId) {
2023-06-05 13:02:36 +02:00
// When we navigate to this page, we always want to be redirected if submit is true and the parameters are valid.
// For programmatic verification, the /verifyemail API should be used.
submitCodeAndContinue({ code });
2023-05-22 11:48:18 +02:00
}
}, []);
2023-05-17 17:04:56 +02:00
const [error, setError] = useState<string>("");
const [loading, setLoading] = useState<boolean>(false);
const router = useRouter();
2024-10-21 10:54:53 +02:00
const params = new URLSearchParams({});
if (loginName) {
params.append("loginName", loginName);
}
if (sessionId) {
params.append("sessionId", sessionId);
}
if (authRequestId) {
params.append("authRequestId", authRequestId);
}
if (organization) {
params.append("organization", organization);
}
async function resendCode() {
2023-05-17 17:04:56 +02:00
setLoading(true);
const response = await resendVerifyEmail({
userId,
2024-09-20 10:21:22 +02:00
}).catch(() => {
setError("Could not resend email");
2023-05-17 17:04:56 +02:00
});
2023-06-05 13:02:36 +02:00
setLoading(false);
return response;
2023-05-22 11:48:18 +02:00
}
2023-05-17 17:04:56 +02:00
async function submitCodeAndContinue(value: Inputs): Promise<boolean | void> {
2023-05-22 11:48:18 +02:00
setLoading(true);
2024-10-21 10:54:53 +02:00
const verifyResponse = await verifyUser({
code: value.code,
userId,
2024-10-21 10:54:53 +02:00
isInvite: isInvite,
2024-09-20 10:21:22 +02:00
}).catch(() => {
setError("Could not verify email");
2023-05-22 11:48:18 +02:00
});
setLoading(false);
2023-05-22 11:48:18 +02:00
if (!verifyResponse) {
setError("Could not verify email");
2024-09-04 15:06:16 +02:00
return;
2023-05-22 11:48:18 +02:00
}
2023-05-17 17:04:56 +02:00
2024-10-21 10:54:53 +02:00
if (verifyResponse.authMethodTypes) {
setAuthMethods(verifyResponse.authMethodTypes);
}
const params = new URLSearchParams({});
if (organization) {
params.set("organization", organization);
}
if (authRequestId && sessionId) {
params.set("authRequest", authRequestId);
params.set("sessionId", sessionId);
return router.push(`/login?` + params);
} else {
return router.push(`/loginname?` + params);
}
2023-05-17 17:04:56 +02:00
}
2024-10-21 10:54:53 +02:00
return !authMethods ? (
2023-05-17 17:04:56 +02:00
<form className="w-full">
<div className="">
<TextInput
type="text"
autoComplete="one-time-code"
{...register("code", { required: "This field is required" })}
label="Code"
2023-06-07 15:34:06 +02:00
// error={errors.username?.message as string}
2023-05-17 17:04:56 +02:00
/>
</div>
2023-05-22 11:48:18 +02:00
{error && (
<div className="py-4">
<Alert>{error}</Alert>
</div>
)}
2023-05-17 17:04:56 +02:00
<div className="mt-8 flex w-full flex-row items-center">
2023-05-22 11:48:18 +02:00
<Button
type="button"
onClick={() => resendCode()}
variant={ButtonVariants.Secondary}
>
2024-10-10 16:15:10 +02:00
{t("resendCode")}
2023-05-22 11:48:18 +02:00
</Button>
2023-05-17 17:04:56 +02:00
<span className="flex-grow"></span>
<Button
type="submit"
className="self-end"
variant={ButtonVariants.Primary}
disabled={loading || !formState.isValid}
onClick={handleSubmit(submitCodeAndContinue)}
>
{loading && <Spinner className="h-5 w-5 mr-2" />}
2024-10-10 16:15:10 +02:00
{t("submit")}
2023-05-17 17:04:56 +02:00
</Button>
</div>
</form>
2024-10-21 10:54:53 +02:00
) : (
<div className="grid grid-cols-1 gap-5 w-full pt-4">
{!authMethods.includes(AuthenticationMethodType.PASSWORD) &&
PASSWORD(false, "/password/set?" + params)}
{!authMethods.includes(AuthenticationMethodType.PASSKEY) &&
PASSKEYS(false, "/passkeys/set?" + params)}
</div>
2023-05-17 17:04:56 +02:00
);
}