"use client" import { useState, useEffect } from "react" import { Mail, CheckCircle2, Loader2, ArrowLeft, } from "lucide-react" import { authClient } from "@/lib/auth-client" import { resetPasswordSchema, } from "@/lib/auth/validation" import OtpInput from "./otp-input" import PasswordStrengthMeter from "./password-strength" import Link from "next/link" interface ResetPasswordFormProps { email: string } export default function ResetPasswordForm({ email, }: ResetPasswordFormProps) { const [otp, setOtp] = useState("") const [newPassword, setNewPassword] = useState("") const [confirmPassword, setConfirmPassword] = useState("") const [error, setError] = useState("") const [isLoading, setIsLoading] = useState(false) const [isSuccess, setIsSuccess] = useState(false) const [resendTimer, setResendTimer] = useState(300) const canResend = resendTimer <= 0 // Countdown timer useEffect(() => { if (resendTimer <= 0) return const interval = setInterval(() => { setResendTimer((prev) => prev - 1) }, 1000) return () => clearInterval(interval) }, [resendTimer]) const formatTime = (seconds: number) => { const mins = Math.floor(seconds / 60) const secs = seconds % 60 return `${mins}:${secs.toString().padStart(2, "0")}` } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError("") const result = resetPasswordSchema.safeParse({ otp, newPassword, confirmPassword, }) if (!result.success) { setError(result.error.issues[0].message) return } setIsLoading(true) const { error } = await authClient.emailOtp.resetPassword({ email, otp, password: newPassword, }) setIsLoading(false) if (error) { setError( error.code === "TOO_MANY_ATTEMPTS" ? "Too many attempts. Please request a new code." : error.message || "Failed to reset password.", ) return } setIsSuccess(true) } const handleResend = async () => { setError("") await authClient.emailOtp.requestPasswordReset({ email }) setResendTimer(300) } if (isSuccess) { return (
Your password has been updated. You can now sign in with your new password.
Sign in