diff --git a/app/login/page.tsx b/app/login/page.tsx index 56005b8..bed685c 100644 --- a/app/login/page.tsx +++ b/app/login/page.tsx @@ -1,23 +1,91 @@ "use client"; import Link from 'next/link'; +import { useState } from 'react'; +import { useRouter } from 'next/navigation'; export default function LoginPage() { + const router = useRouter(); + + 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 username/email and password.'); + return; + } + + setLoading(true); + try { + const res = await fetch('http://localhost:3050/api/auth/login', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ email, password }), + }); + + const contentType = res.headers.get('content-type') || ''; + const data = contentType.includes('application/json') ? await res.json() : await res.text(); + + if (!res.ok) { + throw new Error((typeof data === 'object' && (data?.message || data?.error)) || `Login failed (${res.status})`); + } + + try { + sessionStorage.setItem('USERID', data.userid); + localStorage.setItem('vgproducts_uid', data.userid); + localStorage.setItem('d4a_email', data.email); + } catch { + console.log('Error setting storage'); + } + + setMsg('Login successful!'); + setTimeout(() => router.push('/'), 500); // Redirect to homepage or dashboard after login + } catch (e: any) { + setErr(e?.message || 'Something went wrong. Please try again.'); + } finally { + setLoading(false); + } + }; + return (

Login

Access your pricing and order history.

- -
e.preventDefault()}> + + {err &&
{err}
} + {msg &&
{msg}
} + +
- - + + setEmail(e.target.value)} + />
- + setPassword(e.target.value)} + />
- +
@@ -25,10 +93,12 @@ export default function LoginPage() {
Forgot Password?
- - + +
- + {/*
Don't have a contractor account?
Apply for contractor pricing