161 lines
6.2 KiB
TypeScript
161 lines
6.2 KiB
TypeScript
"use client";
|
|
|
|
import Link from "next/link";
|
|
import { useRouter } from "next/navigation";
|
|
import { useState } from "react";
|
|
import { ContactSection } from "../../components/contact-section";
|
|
import { DemoCta } from "../../components/demo-cta";
|
|
import { FaqSection } from "../../components/faq-section";
|
|
import { PageSchema } from "../../components/page-schema";
|
|
import { SiteFooter } from "../../components/site-footer";
|
|
import { SiteHeader } from "../../components/site-header";
|
|
import { defaultFaqs } from "../../data/faq";
|
|
import { siteInfo } from "../../data/site";
|
|
|
|
type ApiResponse<T> = {
|
|
data: T;
|
|
meta: { timestamp: string; version: "v1" };
|
|
error: null | { message: string; code?: string };
|
|
};
|
|
|
|
type AuthData = { user: { id: string; email: string }; token: string };
|
|
|
|
export default function LoginPage() {
|
|
const router = useRouter();
|
|
const [email, setEmail] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [status, setStatus] = useState<string>("");
|
|
const schema = [
|
|
{
|
|
"@context": "https://schema.org",
|
|
"@type": "WebPage",
|
|
name: "LedgerOne Login",
|
|
description: "Sign in to LedgerOne to access your audit-ready ledger.",
|
|
url: `${siteInfo.url}/login`
|
|
},
|
|
{
|
|
"@context": "https://schema.org",
|
|
"@type": "FAQPage",
|
|
mainEntity: defaultFaqs.map((item) => ({
|
|
"@type": "Question",
|
|
name: item.question,
|
|
acceptedAnswer: { "@type": "Answer", text: item.answer }
|
|
}))
|
|
}
|
|
];
|
|
|
|
const onSubmit = async (event: React.FormEvent) => {
|
|
event.preventDefault();
|
|
setStatus("Signing in...");
|
|
try {
|
|
const res = await fetch("/api/auth/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ email, password })
|
|
});
|
|
const payload = (await res.json()) as ApiResponse<AuthData>;
|
|
if (!res.ok || payload.error) {
|
|
setStatus(payload.error?.message ?? "Login failed.");
|
|
return;
|
|
}
|
|
setStatus(`Welcome back, ${payload.data.user.email}`);
|
|
localStorage.setItem("ledgerone_token", payload.data.token);
|
|
localStorage.setItem("ledgerone_user_id", payload.data.user.id);
|
|
router.push("/app");
|
|
} catch {
|
|
setStatus("Login failed.");
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="min-h-screen bg-background font-sans text-foreground">
|
|
<SiteHeader />
|
|
|
|
<div className="flex min-h-full flex-col justify-center py-12 sm:px-6 lg:px-8 mt-16 relative">
|
|
<div className="absolute inset-0 mesh-gradient -z-10 opacity-30" />
|
|
|
|
<div className="sm:mx-auto sm:w-full sm:max-w-md">
|
|
<div className="flex justify-center">
|
|
<div className="h-12 w-12 rounded-xl bg-primary flex items-center justify-center text-primary-foreground font-bold text-xl shadow-glow-teal">
|
|
L1
|
|
</div>
|
|
</div>
|
|
<h2 className="mt-6 text-center text-3xl font-bold tracking-tight text-foreground">
|
|
Sign in to your account
|
|
</h2>
|
|
<p className="mt-2 text-center text-sm text-muted-foreground">
|
|
Or{" "}
|
|
<Link href="/register" className="font-medium text-primary hover:text-primary/80 transition-colors">
|
|
start your 14-day free trial
|
|
</Link>
|
|
</p>
|
|
</div>
|
|
|
|
<div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
|
|
<div className="glass-panel py-8 px-4 shadow-xl sm:rounded-xl sm:px-10">
|
|
<form className="space-y-6" onSubmit={onSubmit}>
|
|
<div>
|
|
<label htmlFor="email" className="block text-sm font-medium text-foreground">
|
|
Email address
|
|
</label>
|
|
<div className="mt-1">
|
|
<input
|
|
id="email"
|
|
name="email"
|
|
type="email"
|
|
autoComplete="email"
|
|
required
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
className="block w-full appearance-none rounded-lg border border-border bg-background/50 px-3 py-2 placeholder-muted-foreground shadow-sm focus:border-primary focus:outline-none focus:ring-primary sm:text-sm transition-all"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="password" className="block text-sm font-medium text-foreground">
|
|
Password
|
|
</label>
|
|
<div className="mt-1">
|
|
<input
|
|
id="password"
|
|
name="password"
|
|
type="password"
|
|
autoComplete="current-password"
|
|
required
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
className="block w-full appearance-none rounded-lg border border-border bg-background/50 px-3 py-2 placeholder-muted-foreground shadow-sm focus:border-primary focus:outline-none focus:ring-primary sm:text-sm transition-all"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<button
|
|
type="submit"
|
|
className="flex w-full justify-center rounded-lg border border-transparent bg-primary py-2.5 px-4 text-sm font-bold text-primary-foreground shadow-sm hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-2 transition-all hover:-translate-y-0.5"
|
|
>
|
|
Sign in
|
|
</button>
|
|
</div>
|
|
</form>
|
|
|
|
{status && (
|
|
<div className="mt-4 rounded-lg bg-accent/10 border border-accent/20 p-4">
|
|
<div className="flex">
|
|
<div className="ml-3">
|
|
<h3 className="text-sm font-medium text-accent-foreground">{status}</h3>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<SiteFooter />
|
|
<PageSchema schema={schema} />
|
|
</div>
|
|
);
|
|
}
|