'use client'; import React, { useState } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import IconLockDots from '@/components/icon/icon-lock-dots'; import IconMail from '@/components/icon/icon-mail'; const ComponentsAuthLoginForm = () => { const router = useRouter(); const searchParams = useSearchParams(); const nextUrl = searchParams.get('next') || '/'; const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [err, setErr] = useState(null); const [msg, setMsg] = useState(null); const submitForm = async (e: React.FormEvent) => { e.preventDefault(); setErr(null); setMsg(null); if (!email || !password) { setErr('Please enter email and password.'); return; } setLoading(true); try { // ✅ Call your Next.js API (same origin), which sets d4a_uid, d4a_session, d4a_exp cookies const res = await fetch('http://localhost:3003/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, // credentials not required for same-origin, but harmless: credentials: 'same-origin', body: JSON.stringify({ email, password }), }); const contentType = res.headers.get('content-type') || ''; const data = contentType.includes('application/json') ? await res.json() : await res.text(); console.log("data data", data) try { console.log('data:', data.store); sessionStorage.setItem('USERID', data.userid); sessionStorage.setItem('EBAYSTOREID', data.store.urlPath); localStorage.setItem('data4auto_uid', data.userid); localStorage.setItem('d4a_email', data.email); data?.payment?.stripeSessionId && localStorage.setItem('payment_session', data?.payment?.stripeSessionId); console.log('set sessionStorage USERID'); } catch { console.log('no sessionStorage'); } if (!res.ok) { throw new Error((typeof data === 'object' && (data?.message || data?.error)) || `Login failed (${res.status})`); } // (DEV ONLY) quick check that cookies were set: if (process.env.NODE_ENV !== 'production') { try { const who = await fetch('/api/debug/whoami', { cache: 'no-store' }).then((r) => r.json()); console.log('whoami:', who); } catch { } } setMsg('Login successful!'); setTimeout(() => router.push(nextUrl), 500); } catch (e: any) { setErr(e?.message || 'Something went wrong. Please try again.'); } finally { setLoading(false); } }; return (
{/* Alerts */} {err &&
{err}
} {msg &&
{msg}
} {/* Email */}
setEmail(e.target.value)} autoComplete="email" />
{/* Password */}
setPassword(e.target.value)} autoComplete="current-password" />
{/* Optional newsletter */}
{/* */}
); }; export default ComponentsAuthLoginForm;