"use client"; import Input from "@/components/form/input/InputField"; import Label from "@/components/form/Label"; import { ChevronLeftIcon, EyeCloseIcon, EyeIcon } from "@/icons"; import { apiCreateOTP, apiVerifyOTP, apiResetPassword } from "@/service/auth"; import Link from "next/link"; import { useState } from "react"; import { toast } from "sonner"; export default function ResetPasswordForm() { const [step, setStep] = useState(1); const [email, setEmail] = useState(""); const [otp, setOtp] = useState(""); const [newPassword, setNewPassword] = useState(""); const [showPassword, setShowPassword] = useState(false); const [errorMsg, setErrorMsg] = useState(""); const [loading, setLoading] = useState(false); const isValidEmail = (email: string) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; const isValidPassword = (pass: string) => { const passwordRegex = /^(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$/; return passwordRegex.test(pass); }; const handleSendOtp = async (e: React.FormEvent) => { e.preventDefault(); setErrorMsg(""); if (!email.trim()) { setErrorMsg("Vui lòng nhập email."); return; } if (!isValidEmail(email)) { setErrorMsg("Email không đúng định dạng."); return; } try { setLoading(true); await apiCreateOTP(email, 1); toast.success("Mã OTP đã được gửi đến email của bạn!"); setStep(2); } catch (error) { setErrorMsg("Lỗi khi gửi OTP. Vui lòng kiểm tra lại email."); toast.error("Gửi OTP thất bại."); } finally { setLoading(false); } }; const handleResetPassword = async (e: React.FormEvent) => { e.preventDefault(); setErrorMsg(""); if (!otp.trim()) { setErrorMsg("Vui lòng nhập mã OTP."); return; } if (!isValidPassword(newPassword)) { setErrorMsg("Mật khẩu chưa đủ điều kiện bảo mật."); return; } try { setLoading(true); const verifyRes = await apiVerifyOTP(email, otp, 1); const tokenId = verifyRes?.data?.token_id; if (!tokenId) { throw new Error("OTP không hợp lệ hoặc đã hết hạn."); } const resetPayload = { email: email, new_password: newPassword, token_id: tokenId, }; await apiResetPassword(resetPayload); toast.success("Đổi mật khẩu thành công!"); window.location.href = "/signin"; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Đổi mật khẩu thất bại."; setErrorMsg(errorMessage); console.error("Reset password error:", error); toast.error("Vui lòng kiểm tra lại mã OTP."); } finally { setLoading(false); } }; return (
Back to Sign In

{step === 1 ? "Forgot Password" : "Set New Password"}

{step === 1 ? "Enter your email address to receive an OTP code." : `We sent an OTP to ${email}. Please enter it along with your new password.`}

{errorMsg && (
{errorMsg}
)} {step === 1 && (
{ setEmail(e.target.value); setErrorMsg(""); }} placeholder="Enter your registered email" />
)} {step === 2 && (
{ setOtp(e.target.value); setErrorMsg(""); }} placeholder="Enter the 6-digit code" />
0 && !isValidPassword(newPassword) ? "border border-red-500 ring-1 ring-red-500 rounded-lg" : ""}`} > { setNewPassword(e.target.value); setErrorMsg(""); }} placeholder="Min. 8 characters" type={showPassword ? "text" : "password"} /> setShowPassword(!showPassword)} className="absolute z-30 -translate-y-1/2 cursor-pointer right-4 top-1/2" > {showPassword ? : }

Mật khẩu phải chứa tối thiểu 8 ký tự, 1 chữ cái in hoa, 1 chữ số và 1 ký tự đặc biệt.

)}
); }