import { useState } from 'react'; import { Alert, Anchor, Button, Center, Group, Stack, Text, TextInput, ThemeIcon, Title, } from '@mantine/core'; import { IconArrowLeft, IconMail, IconMailForward, IconSend, } from '@tabler/icons-react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { Link } from 'react-router-dom'; import { useApiMutation } from '@ema-platform/api'; import { notify, useErrorHandler } from '@ema-platform/ui'; import { AuthShell } from '../components/AuthShell'; import { useAuthConfig } from '../AuthConfig'; const schema = z.object({ email: z.string().email({ message: 'Enter a valid email' }), }); type FormValues = z.infer; export function ForgotPasswordPage() { const { appName } = useAuthConfig(); const [forgotTrigger, { isLoading }] = useApiMutation(); const [sentTo, setSentTo] = useState(null); const [serverError, setServerError] = useState(null); const { handleError } = useErrorHandler(); const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(schema), }); const sendResetLink = async (email: string) => { await forgotTrigger({ url: '/auth/forgot-password', method: 'POST', body: { email }, }).unwrap(); setSentTo(email); }; const onSubmit = async (values: FormValues) => { try { await sendResetLink(values.email); } catch (err: unknown) { setServerError(handleError(err)); } }; const handleResend = async () => { if (!sentTo || isLoading) return; try { await sendResetLink(sentTo); notify.success('Reset link sent again'); } catch (err: unknown) { setServerError(handleError(err)); } }; const backToSignIn = (
Back to sign in
); if (sentTo) { return (
Check your email We've sent a password reset link to{' '} {sentTo} . Follow the link in that email to choose a new password.
Didn't get the email? Resend link {backToSignIn}
); } return (
Forgot your password? Enter the email linked to your account and we'll send you a link to reset your password.
{serverError && ( setServerError(null)}> {serverError} )}
} error={errors.email?.message} {...register('email')} />
{backToSignIn}
); }