"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 { apiGetCurrentUser, apiSignIn } from "@/service/auth"; import Link from "next/link"; import React, { useState } from "react"; import { toast } from "sonner"; import { API, HOME_URL } from "../../../api"; import { setUserData } from "@/store/features/userSlice"; import { useDispatch } from "react-redux"; import { useRouter } from "next/navigation"; export default function SignInForm() { const router = useRouter(); const [showPassword, setShowPassword] = useState(false); const dispatch = useDispatch(); const [isChecked, setIsChecked] = useState(false); const [errorMsg, setErrorMsg] = useState(""); const [loading, setLoading] = useState(false); const [formData, setFormData] = useState({ email: "", password: "", }); const isFormEmpty = !formData.email.trim() || !formData.password.trim(); 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 handleSignInClick = async (e: React.FormEvent) => { e.preventDefault(); if (loading || isFormEmpty) return; setErrorMsg(""); if (!isValidEmail(formData.email)) { setErrorMsg("Email không đúng định dạng."); return; } if (!isValidPassword(formData.password)) { setErrorMsg( "Mật khẩu tối thiểu 8 ký tự, 1 in hoa, 1 số và 1 ký tự đặc biệt.", ); return; } try { setLoading(true); const res = await apiSignIn(formData); // console.log("API Sign In Response:", res); if (res.status === true) { toast.success("Đăng nhập thành công!"); const data = await apiGetCurrentUser(); // console.log("Current User Data:", data); if (data?.data) { dispatch(setUserData(data.data)); router.push("/"); } } else { toast.error("Email hoặc mật khẩu không đúng."); } } catch (error) { setErrorMsg("Lỗi khi đăng nhập. Vui lòng thử lại."); toast.error("Đăng nhập thất bại. Vui lòng kiểm tra lại thông tin."); } finally { setLoading(false); } }; return (
Back to dashboard

Sign In

Enter your email and password to sign in!

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 ? : }
{errorMsg && (

{errorMsg}

)}
Keep me logged in
Forgot password?

Don't have an account? {""} Sign Up

); }