'use client'; //=========================================== // THIS FILE IS AUTO-GENERATED FROM TEMPLATE. DO NOT EDIT IT DIRECTLY UNLESS YOU ALSO EDIT THE CORRESPONDING FILE IN packages/template //=========================================== import { yupResolver } from "@hookform/resolvers/yup"; import { KnownErrors } from "@hexclave/shared"; import { getPasswordError } from "@hexclave/shared/dist/helpers/password"; import { passwordSchema, yupObject, yupString } from "@hexclave/shared/dist/schema-fields"; import { cacheFunction } from "@hexclave/shared/dist/utils/caches"; import { runAsynchronouslyWithAlert } from "@hexclave/shared/dist/utils/promises"; import { use } from "@hexclave/shared/dist/utils/react"; import { Button, Label, PasswordInput, Typography, cn } from "@hexclave/ui"; import { useState } from "react"; import { useForm } from "react-hook-form"; import * as yup from "yup"; import { StackClientApp, useStackApp } from ".."; import { FormWarningText } from "../components/elements/form-warning"; import { MaybeFullPage } from "../components/elements/maybe-full-page"; import { MessageCard } from "../components/message-cards/message-card"; import { PredefinedMessageCard } from "../components/message-cards/predefined-message-card"; import { useTranslation } from "../lib/translations"; export default function PasswordResetForm(props: { code: string, fullPage?: boolean, }) { const { t } = useTranslation(); const schema = yupObject({ password: passwordSchema.defined(t("Please enter your password")).nonEmpty(t("Please enter your password")).test({ name: 'is-valid-password', test: (value, ctx) => { const error = getPasswordError(value); if (error) { return ctx.createError({ message: error.message }); } else { return true; } } }), passwordRepeat: yupString().nullable().oneOf([yup.ref('password'), null], t("Passwords do not match")).defined().nonEmpty(t("Please repeat your password")) }); const { register, handleSubmit, formState: { errors }, clearErrors } = useForm({ resolver: yupResolver(schema) }); const hexclaveApp = useStackApp(); const [finished, setFinished] = useState(false); const [resetError, setResetError] = useState(false); const [loading, setLoading] = useState(false); const onSubmit = async (data: yup.InferType) => { setLoading(true); try { const { password } = data; const result = await hexclaveApp.resetPassword({ password, code: props.code }); if (result.status === 'error') { setResetError(true); return; } setFinished(true); } finally { setLoading(false); } }; if (finished) { return ; } if (resetError) { return ( {t("Failed to reset password. Please request a new password reset link")} ); } return (
{t("Reset Your Password")}
runAsynchronouslyWithAlert(handleSubmit(onSubmit)(e))} noValidate > { clearErrors('password'); clearErrors('passwordRepeat'); } })} /> { clearErrors('password'); clearErrors('passwordRepeat'); } })} />
); } const cachedVerifyPasswordResetCode = cacheFunction(async (hexclaveApp: StackClientApp, code: string) => { return await hexclaveApp.verifyPasswordResetCode(code); }); export function PasswordReset({ searchParams, fullPage = false, }: { searchParams: Record, fullPage?: boolean, }) { const { t } = useTranslation(); const hexclaveApp = useStackApp(); const invalidJsx = ( {t("Please double check if you have the correct password reset link.")} ); const expiredJsx = ( {t("Your password reset link has expired. Please request a new password reset link from the login page.")} ); const usedJsx = ( {t("This password reset link has already been used. If you need to reset your password again, please request a new password reset link from the login page.")} ); const code = searchParams.code; if (!code) { return invalidJsx; } const result = use(cachedVerifyPasswordResetCode(hexclaveApp, code)); if (result.status === 'error') { if (KnownErrors.VerificationCodeNotFound.isInstance(result.error)) { return invalidJsx; } else if (KnownErrors.VerificationCodeExpired.isInstance(result.error)) { return expiredJsx; } else if (KnownErrors.VerificationCodeAlreadyUsed.isInstance(result.error)) { return usedJsx; } else { throw result.error; } } return ; }