'use client' import { cn } from '@/lib/utils' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { useRouter, useSearchParams } from 'next/navigation' import { useState } from 'react' import resetPassword from '@/serverActions/ResetPassword' import { toast } from 'sonner' import forgotPassword from '@/serverActions/ForgotPassword' export function ForgotPasswordForm({ className, ...props }: React.ComponentProps<'div'>) { const router = useRouter() const [isLoading, setIsLoading] = useState(false) const [didSendForgetRequest, setDidSendForgetRequest] = useState(false) const searchParams = useSearchParams() const token = searchParams.get('token') const handleResetPasswordSubmit = async (e: React.FormEvent) => { e.preventDefault() if (isLoading) return setIsLoading(true) const formData = new FormData(e.currentTarget) const password = String(formData.get('password')) const confirmPassword = String(formData.get('confirmPassword')) if (!password || !confirmPassword || password !== confirmPassword) { toast('Finish the form to continue') setIsLoading(false) return } if (!token) { toast('Password reset token is missing') setIsLoading(false) return } const didReset = await resetPassword({ token, password, confirmPassword, }) console.log('didReset', didReset) if (!didReset) { toast('Issue resetting your password') setIsLoading(false) } else router.push('/login') } const handleForgotPasswordSubmit = async (e: React.FormEvent) => { e.preventDefault() if (isLoading) return setIsLoading(true) const formData = new FormData(e.currentTarget) const email = String(formData.get('email')) if (!email) return const didForget = await forgotPassword({ email }) if (didForget) { toast('A password change email was sent') setDidSendForgetRequest(true) } else toast('There was an issue with your forget password request') setIsLoading(false) } return (
Welcome back {!token ? 'An email to reset your password will be sent if we find it in our system' : 'Please enter your new password'}
{!token ? (
{didSendForgetRequest ? (

Your request has been sent. Check you emails inbox and span folders

) : ( <> )}
) : (
)} {!didSendForgetRequest && ( )}
By clicking Rest or Forgot Password, you agree to our{' '} Terms of Service and Privacy Policy.
) }