2024-04-04 13:50:54 +02:00
|
|
|
"use client";
|
|
|
|
|
|
2024-09-05 15:54:01 +02:00
|
|
|
import { updateSession } from "@/lib/server/session";
|
|
|
|
|
import { create } from "@zitadel/client";
|
|
|
|
|
import { RequestChallengesSchema } from "@zitadel/proto/zitadel/session/v2/challenge_pb";
|
|
|
|
|
import { ChecksSchema } from "@zitadel/proto/zitadel/session/v2/session_service_pb";
|
2024-10-09 17:03:03 +02:00
|
|
|
import { useTranslations } from "next-intl";
|
2024-04-04 13:50:54 +02:00
|
|
|
import { useRouter } from "next/navigation";
|
2024-09-05 13:48:33 +02:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2024-04-17 14:14:25 +02:00
|
|
|
import { useForm } from "react-hook-form";
|
2024-09-26 22:50:55 -04:00
|
|
|
import { Alert, AlertType } from "./alert";
|
|
|
|
|
import { BackButton } from "./back-button";
|
|
|
|
|
import { Button, ButtonVariants } from "./button";
|
|
|
|
|
import { TextInput } from "./input";
|
|
|
|
|
import { Spinner } from "./spinner";
|
2024-04-04 13:50:54 +02:00
|
|
|
|
2024-04-17 14:14:25 +02:00
|
|
|
// either loginName or sessionId must be provided
|
2024-04-04 13:50:54 +02:00
|
|
|
type Props = {
|
2024-04-17 14:14:25 +02:00
|
|
|
loginName?: string;
|
|
|
|
|
sessionId?: string;
|
2024-04-04 13:50:54 +02:00
|
|
|
authRequestId?: string;
|
|
|
|
|
organization?: string;
|
2024-04-17 14:47:52 +02:00
|
|
|
method: string;
|
2024-04-17 14:14:25 +02:00
|
|
|
code?: string;
|
2024-04-04 13:50:54 +02:00
|
|
|
};
|
|
|
|
|
|
2024-04-17 14:14:25 +02:00
|
|
|
type Inputs = {
|
|
|
|
|
code: string;
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-26 22:50:55 -04:00
|
|
|
export function LoginOTP({
|
2024-04-04 13:50:54 +02:00
|
|
|
loginName,
|
2024-04-17 14:14:25 +02:00
|
|
|
sessionId,
|
2024-04-04 13:50:54 +02:00
|
|
|
authRequestId,
|
|
|
|
|
organization,
|
2024-04-17 14:14:25 +02:00
|
|
|
method,
|
|
|
|
|
code,
|
2024-04-04 13:50:54 +02:00
|
|
|
}: Props) {
|
2024-10-09 17:03:03 +02:00
|
|
|
const t = useTranslations("otp");
|
|
|
|
|
|
2024-04-17 14:14:25 +02:00
|
|
|
const [error, setError] = useState<string>("");
|
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
const initialized = useRef(false);
|
|
|
|
|
|
2024-04-04 13:50:54 +02:00
|
|
|
const { register, handleSubmit, formState } = useForm<Inputs>({
|
|
|
|
|
mode: "onBlur",
|
|
|
|
|
defaultValues: {
|
|
|
|
|
code: code ? code : "",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-04-17 14:14:25 +02:00
|
|
|
useEffect(() => {
|
2024-04-17 14:47:52 +02:00
|
|
|
if (!initialized.current && ["email", "sms"].includes(method)) {
|
2024-04-17 14:14:25 +02:00
|
|
|
initialized.current = true;
|
|
|
|
|
setLoading(true);
|
2024-04-17 14:47:52 +02:00
|
|
|
updateSessionForOTPChallenge()
|
|
|
|
|
.then((response) => {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
setError(error);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
});
|
2024-04-17 14:14:25 +02:00
|
|
|
}
|
|
|
|
|
}, []);
|
2024-04-04 13:50:54 +02:00
|
|
|
|
2024-04-17 14:14:25 +02:00
|
|
|
async function updateSessionForOTPChallenge() {
|
2024-09-05 14:49:48 +02:00
|
|
|
let challenges;
|
2024-04-17 14:47:52 +02:00
|
|
|
|
|
|
|
|
if (method === "email") {
|
2024-09-05 14:49:48 +02:00
|
|
|
challenges = create(RequestChallengesSchema, {
|
|
|
|
|
otpEmail: { deliveryType: { case: "sendCode", value: {} } },
|
|
|
|
|
});
|
2024-04-17 14:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (method === "sms") {
|
2024-09-05 14:49:48 +02:00
|
|
|
challenges = create(RequestChallengesSchema, {
|
|
|
|
|
otpSms: { returnCode: true },
|
|
|
|
|
});
|
2024-04-17 14:47:52 +02:00
|
|
|
}
|
2024-09-05 14:49:48 +02:00
|
|
|
|
2024-04-17 14:14:25 +02:00
|
|
|
setLoading(true);
|
2024-09-05 14:49:48 +02:00
|
|
|
const response = await updateSession({
|
|
|
|
|
loginName,
|
|
|
|
|
sessionId,
|
|
|
|
|
organization,
|
|
|
|
|
challenges,
|
|
|
|
|
authRequestId,
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
setError(error.message ?? "Could not request OTP challenge");
|
|
|
|
|
setLoading(false);
|
2024-04-17 14:14:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setLoading(false);
|
2024-09-05 14:49:48 +02:00
|
|
|
|
|
|
|
|
return response;
|
2024-04-17 14:14:25 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-04 13:50:54 +02:00
|
|
|
async function submitCode(values: Inputs, organization?: string) {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
|
|
|
|
|
let body: any = {
|
|
|
|
|
code: values.code,
|
2024-04-16 15:33:14 +02:00
|
|
|
method,
|
2024-04-04 13:50:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (organization) {
|
|
|
|
|
body.organization = organization;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (authRequestId) {
|
|
|
|
|
body.authRequestId = authRequestId;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 14:49:48 +02:00
|
|
|
let checks;
|
|
|
|
|
|
2024-04-17 14:14:25 +02:00
|
|
|
if (method === "sms") {
|
2024-09-05 14:49:48 +02:00
|
|
|
checks = create(ChecksSchema, {
|
|
|
|
|
otpSms: { code: values.code },
|
|
|
|
|
});
|
2024-04-17 14:14:25 +02:00
|
|
|
}
|
|
|
|
|
if (method === "email") {
|
2024-09-05 14:49:48 +02:00
|
|
|
checks = create(ChecksSchema, {
|
|
|
|
|
otpEmail: { code: values.code },
|
|
|
|
|
});
|
2024-04-17 14:14:25 +02:00
|
|
|
}
|
|
|
|
|
if (method === "time-based") {
|
2024-09-05 14:49:48 +02:00
|
|
|
checks = create(ChecksSchema, {
|
|
|
|
|
totp: { code: values.code },
|
|
|
|
|
});
|
2024-04-17 14:14:25 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-05 14:49:48 +02:00
|
|
|
const response = await updateSession({
|
|
|
|
|
loginName,
|
|
|
|
|
sessionId,
|
|
|
|
|
organization,
|
|
|
|
|
checks,
|
|
|
|
|
authRequestId,
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
setError(error.message ?? "Could not verify OTP code");
|
|
|
|
|
setLoading(false);
|
2024-04-04 13:50:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
2024-09-05 14:49:48 +02:00
|
|
|
return response;
|
2024-04-04 13:50:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setCodeAndContinue(values: Inputs, organization?: string) {
|
|
|
|
|
return submitCode(values, organization).then((response) => {
|
|
|
|
|
if (authRequestId && response && response.sessionId) {
|
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
|
sessionId: response.sessionId,
|
|
|
|
|
authRequest: authRequestId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (organization) {
|
|
|
|
|
params.append("organization", organization);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 16:47:07 +02:00
|
|
|
if (authRequestId) {
|
|
|
|
|
params.append("authRequest", authRequestId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sessionId) {
|
|
|
|
|
params.append("sessionId", sessionId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-04 13:50:54 +02:00
|
|
|
return router.push(`/login?` + params);
|
|
|
|
|
} else {
|
2024-09-05 14:49:48 +02:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
if (response?.factors?.user?.loginName) {
|
|
|
|
|
params.append("loginName", response.factors.user.loginName);
|
|
|
|
|
}
|
|
|
|
|
if (authRequestId) {
|
|
|
|
|
params.append("authRequestId", authRequestId);
|
|
|
|
|
}
|
2024-04-04 13:50:54 +02:00
|
|
|
|
|
|
|
|
if (organization) {
|
|
|
|
|
params.append("organization", organization);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return router.push(`/signedin?` + params);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form className="w-full">
|
2024-05-07 14:04:58 +02:00
|
|
|
{["email", "sms"].includes(method) && (
|
|
|
|
|
<Alert type={AlertType.INFO}>
|
|
|
|
|
<div className="flex flex-row">
|
|
|
|
|
<span className="flex-1 mr-auto text-left">
|
2024-10-17 10:06:17 +02:00
|
|
|
{t("verify.noCodeReceived")}
|
2024-05-07 14:04:58 +02:00
|
|
|
</span>
|
|
|
|
|
<button
|
|
|
|
|
aria-label="Resend OTP Code"
|
|
|
|
|
disabled={loading}
|
|
|
|
|
className="ml-4 text-primary-light-500 dark:text-primary-dark-500 hover:dark:text-primary-dark-400 hover:text-primary-light-400 cursor-pointer disabled:cursor-default disabled:text-gray-400 dark:disabled:text-gray-700"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
updateSessionForOTPChallenge()
|
|
|
|
|
.then((response) => {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
setError(error);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-10-17 10:06:17 +02:00
|
|
|
{t("verify.resendCode")}
|
2024-05-07 14:04:58 +02:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</Alert>
|
|
|
|
|
)}
|
|
|
|
|
<div className="mt-4">
|
2024-04-04 13:50:54 +02:00
|
|
|
<TextInput
|
|
|
|
|
type="text"
|
|
|
|
|
{...register("code", { required: "This field is required" })}
|
|
|
|
|
label="Code"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
|
<div className="py-4">
|
|
|
|
|
<Alert>{error}</Alert>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div className="mt-8 flex w-full flex-row items-center">
|
2024-07-15 16:00:17 +02:00
|
|
|
<BackButton />
|
2024-04-04 13:50:54 +02:00
|
|
|
<span className="flex-grow"></span>
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
className="self-end"
|
|
|
|
|
variant={ButtonVariants.Primary}
|
|
|
|
|
disabled={loading || !formState.isValid}
|
2024-08-14 08:23:48 +02:00
|
|
|
onClick={handleSubmit((e) => {
|
|
|
|
|
setCodeAndContinue(e, organization);
|
|
|
|
|
})}
|
2024-04-04 13:50:54 +02:00
|
|
|
>
|
|
|
|
|
{loading && <Spinner className="h-5 w-5 mr-2" />}
|
2024-10-17 10:06:17 +02:00
|
|
|
{t("verify.submit")}
|
2024-04-04 13:50:54 +02:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|