'use client'; import React, { useState } from 'react'; import { useRouter } from 'next/navigation'; import IconLockDots from '@/components/icon/icon-lock-dots'; import IconMail from '@/components/icon/icon-mail'; import IconUser from '@/components/icon/icon-user'; const API_BASE = process.env.NEXT_PUBLIC_API_BASE_URL?.replace(/\/$/, '') || // 'https://ebay.backend.data4autos.com'; 'http://localhost:3003'; const ComponentsAuthRegisterForm = () => { const router = useRouter(); const [name, setName] = useState(''); const [phonenumber, setPhonenumber] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [subscribe, setSubscribe] = useState(false); 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 (!name || !email || !password || !phonenumber) { setErr('Please fill in all fields.'); return; } setLoading(true); try { const res = await fetch(`${API_BASE}/api/auth/signup`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, // If your API sets cookies, uncomment: // credentials: 'include', body: JSON.stringify({ name, email, password, phonenumber, // You can also send `subscribe` if your API supports it // subscribe, }), }); const contentType = res.headers.get('content-type') || ''; let data: any = null; if (contentType.includes('application/json')) { data = await res.json(); } else { data = await res.text(); } if (!res.ok) { const message = (typeof data === 'object' && (data?.message || data?.error)) || `Signup failed (${res.status})`; throw new Error(message); } setMsg( (typeof data === 'object' && (data?.message || 'Signup successful!')) || 'Signup successful!' ); // Redirect to login (or wherever you like) setTimeout(() => router.push('/login'), 1000); } catch (e: any) { setErr(e?.message || 'Something went wrong. Please try again.'); } finally { setLoading(false); } }; return (
{/* Name */}
setName(e.target.value)} autoComplete="name" />
{/* Email */}
setEmail(e.target.value)} autoComplete="email" />
{/* Password */}
setPassword(e.target.value)} autoComplete="new-password" />
{/* Phone Number */}
setPhonenumber(e.target.value)} autoComplete="tel" /> {/* Simple emoji icon to match the padded layout without adding a new component */} 📞
{/* Subscribe */} {/*
*/} {/* Alerts */} {err && (
{err}
)} {msg && (
{msg}
)} {/* Submit */}
); }; export default ComponentsAuthRegisterForm;