'use client'; import { useState, useEffect } from 'react'; import Link from 'next/link'; import { usePathname, useSearchParams } from 'next/navigation'; import { useLanguage } from '@/context/LanguageContext'; import { useAuth } from '@/context/AuthContext'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import Input from '@/components/ui/Input'; import GoogleSignInButton from '@/components/GoogleSignInButton'; import { authApi } from '@/lib/api'; import { XMarkIcon } from '@heroicons/react/24/outline'; import toast from 'react-hot-toast'; interface LoginModalProps { open: boolean; onClose: () => void; /** Called after a successful password login (Google logins reload the page). */ onSuccess?: () => void; /** Optional context line shown under the title (e.g. why login is needed). */ message?: string; } /** * Reusable login pop-up: same capabilities as /login (password, * magic link, Google) but inline, so the visitor stays on the page — * used by gated photo galleries. */ export default function LoginModal({ open, onClose, onSuccess, message }: LoginModalProps) { const { t, locale } = useLanguage(); const es = locale === 'es'; const { login } = useAuth(); const pathname = usePathname(); const searchParams = useSearchParams(); const [mode, setMode] = useState<'password' | 'magic-link'>('password'); const [magicLinkSent, setMagicLinkSent] = useState(false); const [loading, setLoading] = useState(false); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); // Google logins hard-reload; send them back to exactly where they are // (including a ?token= share link). const query = searchParams.toString(); const currentUrl = query ? `${pathname}?${query}` : pathname; useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose(); }; window.addEventListener('keydown', onKey); document.body.style.overflow = 'hidden'; return () => { window.removeEventListener('keydown', onKey); document.body.style.overflow = ''; }; }, [open, onClose]); if (!open) return null; const handlePasswordLogin = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { await login(email, password); toast.success(es ? '¡Bienvenido!' : 'Welcome back!'); onClose(); onSuccess?.(); } catch (error) { toast.error(error instanceof Error ? error.message : t('auth.errors.invalidCredentials')); } finally { setLoading(false); } }; const handleMagicLink = async (e: React.FormEvent) => { e.preventDefault(); if (!email) { toast.error(es ? 'Ingresa tu email' : 'Please enter your email'); return; } setLoading(true); try { await authApi.requestMagicLink(email); setMagicLinkSent(true); toast.success(es ? 'Revisa tu correo para el enlace de acceso' : 'Check your email for the login link'); } catch (error) { toast.error(error instanceof Error ? error.message : es ? 'Error' : 'Failed'); } finally { setLoading(false); } }; return (
{message}
}{es ? 'Te enviamos un enlace de acceso. Abre tu correo y vuelve a esta página.' : 'We sent you a login link. Open your email and come back to this page.'}
) : ( )}