import {zodResolver} from '@hookform/resolvers/zod'; import {AuthenticationSchemas} from '@plunk/shared'; import { Button, Card, CardContent, Form, FormControl, FormField, FormItem, FormLabel, FormMessage, IconSpinner, Input, } from '@plunk/ui'; import {AnimatePresence, motion} from 'framer-motion'; import {NextSeo} from 'next-seo'; import Image from 'next/image'; import Link from 'next/link'; import {useRouter} from 'next/router'; import React, {useEffect, useState} from 'react'; import {useForm} from 'react-hook-form'; import type {z} from 'zod'; import {network} from '../../lib/network'; const dotGrid = { backgroundColor: '#fafafa', backgroundImage: 'radial-gradient(#e5e7eb 1px, transparent 1px)', backgroundSize: '20px 20px', }; const Wordmark = () => (
Plunk
); export default function ResetPassword() { const router = useRouter(); const {token} = router.query; const [status, setStatus] = useState<'idle' | 'success' | 'error'>('idle'); const [errorMessage, setErrorMessage] = useState(''); const form = useForm>({ resolver: zodResolver(AuthenticationSchemas.resetPassword), defaultValues: { token: '', newPassword: '', }, }); useEffect(() => { if (token && typeof token === 'string') { form.setValue('token', token); } }, [token, form]); async function onSubmit(values: z.infer) { try { const response = await network.fetch<{success: boolean}, typeof AuthenticationSchemas.resetPassword>( 'POST', '/auth/reset-password', values, ); if (response.success) { setStatus('success'); setTimeout(() => { void router.push('/auth/login'); }, 2000); } else { setStatus('error'); setErrorMessage('Failed to reset password. The link may be invalid or expired.'); } } catch (error) { setStatus('error'); setErrorMessage(error instanceof Error ? error.message : 'Something went wrong'); } } if (!token) { return ( <>

Invalid reset link

This link is invalid or has expired. Request a new one from the login page.

); } return ( <>
{status === 'success' ? (

Password updated

Redirecting you to login...

) : (
{ e.preventDefault(); void form.handleSubmit(onSubmit)(e); }} >

Reset your password

Enter your new password below

( New password )} /> {status === 'error' && ( {errorMessage} )}

Remember your password?{' '} Back to login

)}
); }