"use client"; import Checkbox from "@/components/form/input/Checkbox"; import Input from "@/components/form/input/InputField"; import Label from "@/components/form/Label"; import { ChevronLeftIcon, EyeCloseIcon, EyeIcon } from "@/icons"; import { apiCreateOTP, apiSignUp, apiVerifyOTP } from "@/service/auth"; import Link from "next/link"; import { useState } from "react"; import { toast } from "sonner"; import { API, HOME_URL } from "../../../api"; import Swal from "sweetalert2"; import { useRouter } from "next/navigation"; export default function SignUpForm() { const [showPassword, setShowPassword] = useState(false); const [isChecked, setIsChecked] = useState(false); const router = useRouter(); const [step, setStep] = useState(1); const [formData, setFormData] = useState({ fname: "", lname: "", email: "", password: "", }); const [otp, setOtp] = useState(""); const [errorMsg, setErrorMsg] = useState(""); const [loading, setLoading] = useState(false); const handleChange = (e: React.ChangeEvent) => { setFormData({ ...formData, [e.target.name]: e.target.value }); setErrorMsg(""); }; 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 handleSignUpClick = async (e: React.FormEvent) => { e.preventDefault(); setErrorMsg(""); if ( !formData.fname || !formData.lname || !formData.email || !formData.password ) { setErrorMsg("Vui lòng điền đầy đủ thông tin."); return; } if (!isValidEmail(formData.email)) { setErrorMsg("Email không đúng định dạng."); return; } if (!isValidPassword(formData.password)) { setErrorMsg( "Mật khẩu chưa đủ điều kiện. Vui lòng nhập tối thiểu 8 ký tự, 1 in hoa, 1 số và 1 ký tự đặc biệt.", ); return; } try { setLoading(true); await apiCreateOTP(formData.email); setStep(2); } catch (error) { setErrorMsg("Lỗi khi tạo OTP. Vui lòng thử lại."); toast.error("Tạo OTP thất bại. Vui lòng kiểm tra lại thông tin."); } finally { setLoading(false); } }; const handleVerifyOtpSubmit = async (e: React.FormEvent) => { e.preventDefault(); setErrorMsg(""); if (!otp) { setErrorMsg("Vui lòng nhập OTP."); return; } try { setLoading(true); const verifyRes = await apiVerifyOTP(formData.email, otp); const tokenId = verifyRes?.data?.token_id; if (!tokenId) { throw new Error("OTP không hợp lệ hoặc không có token_id"); } const signupPayload = { display_name: `${formData.fname} ${formData.lname}`.trim(), email: formData.email, password: formData.password, token_id: tokenId, }; const signupRes = await apiSignUp(signupPayload); await Swal.fire({ title: "Tạo tài khoản thành công!. Quay về trang đăng nhập để tiếp tục", icon: "success", timer: 1500, showConfirmButton: false, }); router.push("/signin"); } catch (error) { const errorMessage = error instanceof Error ? error.message : "Xác thực OTP hoặc đăng ký thất bại."; setErrorMsg(errorMessage || "Xác thực OTP hoặc đăng ký thất bại."); } finally { setLoading(false); } }; return (
Back to dashboard

{step === 1 ? "Sign Up" : "Verify OTP"}

{step === 1 ? "Enter your email and password to sign up!" : `We sent an OTP to ${formData.email}. Please check your email and enter it below.`}

{errorMsg && (
{errorMsg}
)} {step === 1 && ( <>
Or
0 && !isValidPassword(formData.password) ? "border border-red-500 ring-1 ring-red-500 rounded-lg" : ""}`} > 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.

0 && !isValidPassword(formData.password) ? "opacity-50 cursor-not-allowed" : "" } > { if (isValidPassword(formData.password)) setIsChecked(val); }} disabled={!isValidPassword(formData.password)} />

By creating an account means you agree to the{" "} Terms and Conditions, {" "} and our{" "} Privacy Policy

Already have an account?{" "} Sign In

)} {step === 2 && (
setOtp(e.target.value)} placeholder="Enter the 6-digit code" />
)}
); }