import { Form, useActionData } from '@remix-run/react'; import type { ActionArgs, LoaderArgs } from '@remix-run/server-runtime'; import { json, redirect } from '@remix-run/server-runtime'; import { z } from 'zod'; import { getSignedInUser } from '../auth.server'; import { BackButton } from '../components/BackButton'; import { Button } from '../components/Button'; import { TextInput } from '../components/TextInput'; import { getUserByEmail, sendResetPasswordEmail } from '../data/zippo.server'; import { validateFormData } from '../utils/utils'; const zodEmailModel = z.object({ email: z.string().email(), }); type ActionInput = z.TypeOf; export async function action({ request }: ActionArgs) { const [user, headers] = await getSignedInUser(request); if (user) { throw redirect('/apps', { headers }); } const { body, errors } = validateFormData({ formData: await request.formData(), schema: zodEmailModel, }); if (errors) { return json({ errors, values: body }); } const userRes = await getUserByEmail({ email: body.email }); // we don't want to give away whether the email exists or not if (userRes.result === 'SUCCESS') { await sendResetPasswordEmail({ userId: userRes.data.id, email: body.email }); } throw redirect('/reset-password/email-sent?email=' + encodeURIComponent(body.email)); } export async function loader({ request }: LoaderArgs) { const [user, headers] = await getSignedInUser(request); if (user) { throw redirect('/apps', { headers }); } return null; } export default function ResetPasswordPage() { const actionData = useActionData(); return (

Reset your password

Enter the email address you registered with and we'll send you instructions to reset your password.

); }