Files
zitadel/apps/login/ui/PasswordForm.tsx

150 lines
3.9 KiB
TypeScript
Raw Normal View History

"use client";
import { useState } from "react";
import { Button, ButtonVariants } from "./Button";
import { TextInput } from "./Input";
import { useForm } from "react-hook-form";
import { useRouter } from "next/navigation";
import { Spinner } from "./Spinner";
2023-05-22 16:28:47 +02:00
import Alert from "./Alert";
type Inputs = {
password: string;
};
2023-05-22 16:28:47 +02:00
type Props = {
2023-05-24 14:28:55 +02:00
loginName?: string;
2023-08-22 13:15:33 +02:00
authRequestId?: string;
isAlternative?: boolean; // whether password was requested as alternative auth method
promptPasswordless?: boolean;
2023-05-22 16:28:47 +02:00
};
export default function PasswordForm({
loginName,
2023-08-22 13:15:33 +02:00
authRequestId,
promptPasswordless,
isAlternative,
}: Props) {
const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur",
});
const [error, setError] = useState<string>("");
const [loading, setLoading] = useState<boolean>(false);
const router = useRouter();
async function submitPassword(values: Inputs) {
2023-05-23 11:15:47 +02:00
setError("");
setLoading(true);
2024-03-19 14:15:54 +01:00
2023-06-30 14:13:03 +02:00
const res = await fetch("/api/session", {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
2023-06-29 17:04:34 +02:00
loginName,
password: values.password,
2023-08-22 13:15:33 +02:00
authRequestId,
}),
});
2023-05-22 16:28:47 +02:00
const response = await res.json();
setLoading(false);
if (!res.ok) {
2023-05-22 16:28:47 +02:00
setError(response.details);
return Promise.reject(response.details);
}
return response;
}
function submitPasswordAndContinue(value: Inputs): Promise<boolean | void> {
return submitPassword(value).then((resp: any) => {
if (
resp.factors &&
!resp.factors.passwordless && // if session was not verified with a passkey
promptPasswordless && // if explicitly prompted due policy
!isAlternative // escaped if password was used as an alternative method
) {
2023-06-19 15:59:16 +02:00
return router.push(
`/passkey/add?` +
2023-06-19 16:28:29 +02:00
new URLSearchParams({
loginName: resp.factors.user.loginName,
promptPasswordless: "true",
2023-06-19 16:28:29 +02:00
})
2023-06-19 15:59:16 +02:00
);
} else {
2024-03-06 14:44:48 +01:00
if (authRequestId && resp && resp.sessionId) {
return router.push(
`/login?` +
new URLSearchParams({
sessionId: resp.sessionId,
authRequest: authRequestId,
})
);
} else {
return router.push(
`/signedin?` +
new URLSearchParams(
authRequestId
? {
loginName: resp.factors.user.loginName,
authRequestId,
}
: {
loginName: resp.factors.user.loginName,
}
)
);
}
2023-06-19 15:59:16 +02:00
}
});
}
const { errors } = formState;
return (
<form className="w-full">
2023-05-23 11:15:47 +02:00
<div className={`${error && "transform-gpu animate-shake"}`}>
<TextInput
type="password"
autoComplete="password"
{...register("password", { required: "This field is required" })}
label="Password"
// error={errors.username?.message as string}
/>
2023-05-22 16:28:47 +02:00
{loginName && (
<input type="hidden" name="loginName" value={loginName} />
)}
</div>
2023-05-22 16:28:47 +02:00
{error && (
<div className="py-4">
<Alert>{error}</Alert>
</div>
)}
<div className="mt-8 flex w-full flex-row items-center">
{/* <Button type="button" variant={ButtonVariants.Secondary}>
back
</Button> */}
<span className="flex-grow"></span>
<Button
type="submit"
className="self-end"
variant={ButtonVariants.Primary}
disabled={loading || !formState.isValid}
onClick={handleSubmit(submitPasswordAndContinue)}
>
{loading && <Spinner className="h-5 w-5 mr-2" />}
continue
</Button>
</div>
</form>
);
}