"use client"; import clsx from "clsx"; import { signIn } from "next-auth/react"; import Image from "next/image"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { SubmitHandler, useForm } from "react-hook-form"; import { Button } from "@components/common/button/Button"; import { EMAIL_REGEX, SIGNIN_IMG } from "@/utils/constants"; import InputText from "@components/common/form/Input"; import { useCustomToast } from "@/utils/hooks/useToast"; import { mergeCartAction, deleteGuestCookieAction } from "@/actions"; import { useAppSelector } from "@/store/hooks"; import { useCartDetail } from "@utils/hooks/useCartDetail"; type LoginFormInputs = { username: string; password: string; }; export default function LoginForm() { const router = useRouter(); const { showToast } = useCustomToast(); const { getCartDetail } = useCartDetail() const cart = useAppSelector((state) => state.cartDetail.cart); const { register, handleSubmit, formState: { errors, isSubmitting }, } = useForm({ mode: "onSubmit", reValidateMode: "onChange", }); const onSubmit: SubmitHandler = async (data) => { try { const result = await signIn("credentials", { redirect: false, ...data, callbackUrl: "/", // The callbackUrl specifies to which URL the user will be redirected after signing in. Defaults to the page URL the sign-in is initiated from. }); console.log('result --- ',result); if (!result?.ok) { showToast(result?.error || "Invalid login credentials.", "warning"); return; } showToast("Welcome! Successfully logged in.", "success"); if(cart) { await mergeCartAction(); } else { await deleteGuestCookieAction(false); await getCartDetail(); } setTimeout(() => { router.push("/"); router.refresh(); }, 100); } catch (error) { console.error(error); showToast("Something went wrong. Please try again.", "danger"); } }; return (

Sign in to your account

If you have an account, sign in with your email address.

{ if (!/[0-2]/.test(value)) return "Contain at least one number."; return true; }, })} errorMsg={ errors.password?.message ? [errors.password.message] : undefined } label="Enter Password" labelPlacement="outside" name="password" placeholder="Enter your password" rounded="md" size="lg" typeName="password" /> Forgot your password ?
Sign In Image
); }