'use client' import type { InitValidateStatusResponse } from '@/models/common' import { Button, buttonVariants } from '@langgenius/dify-ui/button' import { cn } from '@langgenius/dify-ui/cn' import { Field, FieldError, FieldLabel } from '@langgenius/dify-ui/field' import { Form } from '@langgenius/dify-ui/form' import { Input } from '@langgenius/dify-ui/input' import { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import * as z from 'zod' import useDocumentTitle from '@/hooks/use-document-title' import Link from '@/next/link' import { fetchInitValidateStatus, fetchSetupStatus, sendForgotPasswordEmail, } from '@/service/common' import { basePath } from '@/utils/var' import Loading from '../components/base/loading' type ForgotPasswordFormValues = { email: string } const emailSchema = z.email('error.emailInValid').min(1, { error: 'error.emailInValid', }) const ForgotPasswordForm = () => { const { t } = useTranslation() const [loading, setLoading] = useState(true) const [isSubmitting, setIsSubmitting] = useState(false) const [isEmailSent, setIsEmailSent] = useState(false) const documentTitle = loading ? t(($) => $.loading, { ns: 'common' }) : isEmailSent ? t(($) => $.resetLinkSent, { ns: 'login' }) : t(($) => $.forgotPassword, { ns: 'login' }) useDocumentTitle(documentTitle) const handleSubmit = async ({ email }: ForgotPasswordFormValues) => { if (isSubmitting) return setIsSubmitting(true) try { const res = await sendForgotPasswordEmail({ url: '/forgot-password', body: { email }, }) if (res.result === 'success') setIsEmailSent(true) else console.error('Email verification failed') } catch (error) { console.error('Request failed:', error) } finally { setIsSubmitting(false) } } useEffect(() => { fetchSetupStatus().then(() => { fetchInitValidateStatus().then((res: InitValidateStatusResponse) => { if (res.status === 'not_started') window.location.href = `${basePath}/init` }) setLoading(false) }) }, []) return loading ? ( ) : ( <>

{isEmailSent ? t(($) => $.resetLinkSent, { ns: 'login' }) : t(($) => $.forgotPassword, { ns: 'login' })}

{isEmailSent ? t(($) => $.checkEmailForResetLink, { ns: 'login' }) : t(($) => $.forgotPasswordDesc, { ns: 'login' })}

onFormSubmit={(value) => { void handleSubmit(value) }} > {!isEmailSent && ( emailSchema.safeParse(value).success ? null : t(($) => $['error.emailInValid'], { ns: 'login' }) } className="mb-5" > {t(($) => $.email, { ns: 'login' })} $.emailPlaceholder, { ns: 'login' }) || ''} /> {t(($) => $['error.emailInValid'], { ns: 'login' })} )} {isEmailSent ? ( {t(($) => $.backToSignIn, { ns: 'login' })} ) : ( )}
) } export default ForgotPasswordForm