'use client'; import { Dialog, Transition } from '@headlessui/react'; import { UserCircleIcon, XMarkIcon } from '@heroicons/react/24/outline'; import { useCart } from 'components/cart/cart-context'; import { signIn, signOut, useSession } from 'next-auth/react'; import { Fragment, useEffect, useState } from 'react'; export default function LoginModal() { const [isOpen, setIsOpen] = useState(false); const [isLogged, setIsLogged] = useState(false); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const openLogin = () => setIsOpen(true); const closeLogin = () => setIsOpen(false); const {setNewCart} = useCart(); const {data} = useSession(); useEffect(() => { if (data?.user.token) { setIsLogged(true); } }, [data]); const handleLogin = async (event: React.FormEvent) => { event.preventDefault(); try { const res = await signIn('credentials', {username, password, redirect: false}); const cart = await (await fetch('/api/cart')).json(); setNewCart(cart); closeLogin(); } catch (error) { console.error(error); } }; return ( <> ); }