'use client'; import React, { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import toast from 'react-hot-toast'; import ForgotPasswordModal from '../auth/ForgotPasswordModal'; import './login.scss'; import { googleLogin } from '@/app/API/auth/GoogleLogin'; interface LoginData { email: string; password: string; rememberMe?: boolean; } interface LoginFormProps { onSubmit: (data: LoginData) => Promise; loading?: boolean; } const LoginForm: React.FC = ({ onSubmit, loading }) => { const router = useRouter(); const [formData, setFormData] = useState({ email: '', password: '', rememberMe: false }); const [showPassword, setShowPassword] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false); const [googleLoading, setGoogleLoading] = useState(false); useEffect(() => { const initializeGoogleSignIn = () => { if (typeof window !== 'undefined' && window.google?.accounts?.id) { google.accounts.id.initialize({ client_id: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID ?? '', callback: handleGoogleResponse, context: 'signin' }); const buttonElement = document.getElementById("googleSignInButton"); if (buttonElement) { google.accounts.id.renderButton( buttonElement, { theme: "filled_blue", size: "large", width: "100%", type: "standard", text: "signin_with", shape: "rectangular", logo_alignment: "left" } ); } } }; initializeGoogleSignIn(); }, []); const handleGoogleResponse = async (response: google.accounts.id.CredentialResponse) => { setGoogleLoading(true); try { const result = await googleLogin(response.credential); if (result.success && result.data) { localStorage.setItem('user', JSON.stringify(result.data.user)); router.push('/profile'); } else { toast.error(result.message || 'Google authentication failed'); } } catch (err) { console.error('Google auth error:', err); toast.error('Google authentication failed'); } finally { setGoogleLoading(false); } }; const handleChange = (e: React.ChangeEvent) => { const value = e.target.type === 'checkbox' ? e.target.checked : e.target.value; setFormData(prev => ({ ...prev, [e.target.name]: value })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); try { await onSubmit(formData); } catch (err) { const message = err instanceof Error ? err.message : 'Login failed'; if (message.includes('Please sign in with Google')) { toast.error('This email is registered with Google. Please use Google Sign-In.'); } else { toast.error(message); } } }; return (

or
Don't have an account? Sign up
setIsModalOpen(false)} />
); }; export default LoginForm;