2026-10-23 19:56:38 -07:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { Button } from '@/components/ui/Button';
|
|
|
|
|
import { FormSwitch } from '@/components/ui/Form';
|
|
|
|
|
import { Input } from '@/components/ui/Input';
|
|
|
|
|
import { ArrowRight } from 'lucide-react';
|
|
|
|
|
import Link from 'next/link';
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
function safeNextPath() {
|
|
|
|
|
const value = new URLSearchParams(window.location.search).get('next');
|
|
|
|
|
return value && value.startsWith('/') && !value.startsWith('//') && value !== '/vcon/login'
|
|
|
|
|
? value
|
|
|
|
|
: '/vcon/dashboard';
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 20:05:00 +06:00
|
|
|
export function AdminLogin({
|
|
|
|
|
showDemoHint = false,
|
2026-11-19 20:22:01 +06:00
|
|
|
databaseReady = true,
|
2026-11-17 20:05:00 +06:00
|
|
|
}: {
|
|
|
|
|
showDemoHint?: boolean;
|
2026-11-19 20:22:01 +06:00
|
|
|
databaseReady?: boolean;
|
2026-11-17 20:05:00 +06:00
|
|
|
}) {
|
2026-11-18 05:18:55 +06:00
|
|
|
const [email, setEmail] = useState('');
|
|
|
|
|
const [password, setPassword] = useState('');
|
2026-10-23 19:56:38 -07:00
|
|
|
const [remember, setRemember] = useState(true);
|
|
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
2026-11-19 20:22:01 +06:00
|
|
|
const [errorMessage, setErrorMessage] = useState(
|
|
|
|
|
databaseReady ? '' : 'Database connection unavailable. Retry once PostgreSQL recovers.',
|
|
|
|
|
);
|
2026-10-23 19:56:38 -07:00
|
|
|
const [theme, setTheme] = useState<'dark' | 'light'>('dark');
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const savedTheme = localStorage.getItem('datastack-theme');
|
|
|
|
|
const initialTheme = savedTheme === 'light' ? 'light' : 'dark';
|
|
|
|
|
setTheme(initialTheme);
|
|
|
|
|
document.documentElement.classList.toggle('light', initialTheme === 'light');
|
|
|
|
|
document.documentElement.classList.toggle('dark', initialTheme === 'dark');
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const toggleTheme = () => {
|
|
|
|
|
const nextTheme = theme === 'dark' ? 'light' : 'dark';
|
|
|
|
|
setTheme(nextTheme);
|
|
|
|
|
localStorage.setItem('datastack-theme', nextTheme);
|
|
|
|
|
document.documentElement.classList.toggle('light', nextTheme === 'light');
|
|
|
|
|
document.documentElement.classList.toggle('dark', nextTheme === 'dark');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const submit = async (event: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
setErrorMessage('');
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch('/api/auth/login', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2026-11-16 22:12:48 +06:00
|
|
|
credentials: 'include',
|
2026-10-23 19:56:38 -07:00
|
|
|
body: JSON.stringify({ email, password, remember }),
|
|
|
|
|
});
|
|
|
|
|
const body = await response.json();
|
|
|
|
|
if (!response.ok) throw new Error(body.error || 'Sign in failed.');
|
2026-11-16 22:12:48 +06:00
|
|
|
const targetPath = safeNextPath();
|
|
|
|
|
window.location.assign(targetPath);
|
|
|
|
|
window.setTimeout(() => {
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
}, 2500);
|
2026-10-23 19:56:38 -07:00
|
|
|
} catch (submitError) {
|
|
|
|
|
setErrorMessage(submitError instanceof Error ? submitError.message : 'Unable to sign in.');
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2026-11-16 21:58:18 +06:00
|
|
|
<main className="relative grid min-h-dvh place-items-center bg-[var(--bg)] p-4 text-[var(--text)]">
|
2026-10-23 19:56:38 -07:00
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="icon"
|
|
|
|
|
size="icon"
|
|
|
|
|
icon={theme === 'dark' ? 'Sun' : 'Moon'}
|
|
|
|
|
aria-label="Toggle theme"
|
|
|
|
|
title="Toggle theme"
|
2026-11-16 21:58:18 +06:00
|
|
|
className="fixed right-3 top-3 z-10 h-7 w-7 border border-[var(--border)] text-[var(--text-muted)]"
|
2026-10-23 19:56:38 -07:00
|
|
|
onClick={toggleTheme}
|
|
|
|
|
/>
|
2026-11-16 21:58:18 +06:00
|
|
|
<section className="z-0 w-full max-w-[300px] border border-[var(--border)] bg-[var(--surface)]">
|
|
|
|
|
<header className="flex items-center justify-between border-b border-[var(--border)] px-4 py-3">
|
|
|
|
|
<Link href="/" className="flex items-center gap-2 text-[12.5px] font-medium text-[var(--text)]">
|
|
|
|
|
<span className="grid h-3 w-3 place-items-center bg-[var(--accent)]" aria-hidden="true">
|
|
|
|
|
<span className="h-1 w-1 bg-[var(--bg)]" />
|
2026-10-23 19:56:38 -07:00
|
|
|
</span>
|
|
|
|
|
DataStack
|
|
|
|
|
</Link>
|
2026-11-16 21:58:18 +06:00
|
|
|
<h1 className="text-[12px] font-medium text-[var(--text-muted)]">Sign in</h1>
|
2026-10-23 19:56:38 -07:00
|
|
|
</header>
|
|
|
|
|
|
2026-11-16 21:58:18 +06:00
|
|
|
<form onSubmit={submit} className="flex flex-col gap-2.5 p-4">
|
2026-10-23 19:56:38 -07:00
|
|
|
{errorMessage && (
|
2026-11-16 21:58:18 +06:00
|
|
|
<div role="alert" className="border border-[var(--border-strong)] bg-[var(--surface-2)] px-2.5 py-1.5 text-[11px] text-[var(--text-muted)]">
|
|
|
|
|
{errorMessage}
|
2026-10-23 19:56:38 -07:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<Input
|
|
|
|
|
label="Email"
|
|
|
|
|
type="email"
|
|
|
|
|
autoComplete="username"
|
2026-11-19 00:42:38 +06:00
|
|
|
placeholder="admin@datastack.dev"
|
2026-10-23 19:56:38 -07:00
|
|
|
value={email}
|
|
|
|
|
onChange={(event) => setEmail(event.target.value)}
|
|
|
|
|
compact
|
|
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
|
2026-11-16 21:58:18 +06:00
|
|
|
<Input
|
|
|
|
|
id="admin-password"
|
|
|
|
|
label="Password"
|
|
|
|
|
type={showPassword ? 'text' : 'password'}
|
|
|
|
|
autoComplete="current-password"
|
|
|
|
|
placeholder="Password"
|
|
|
|
|
value={password}
|
|
|
|
|
onChange={(event) => setPassword(event.target.value)}
|
|
|
|
|
compact
|
|
|
|
|
required
|
|
|
|
|
rightElement={
|
2026-10-23 19:56:38 -07:00
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="icon"
|
|
|
|
|
size="icon"
|
|
|
|
|
icon={showPassword ? 'EyeOff' : 'Eye'}
|
|
|
|
|
aria-label={showPassword ? 'Hide password' : 'Show password'}
|
|
|
|
|
title={showPassword ? 'Hide password' : 'Show password'}
|
2026-11-16 21:58:18 +06:00
|
|
|
className="h-7.5 w-7.5 border-l border-[var(--border-strong)] text-[var(--text-faint)]"
|
2026-10-23 19:56:38 -07:00
|
|
|
onClick={() => setShowPassword((current) => !current)}
|
|
|
|
|
/>
|
2026-11-16 21:58:18 +06:00
|
|
|
}
|
|
|
|
|
/>
|
2026-10-23 19:56:38 -07:00
|
|
|
|
2026-11-16 21:58:18 +06:00
|
|
|
<FormSwitch label="Remember me" checked={remember} onChange={setRemember} />
|
2026-10-23 19:56:38 -07:00
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="primary"
|
|
|
|
|
size="sm"
|
|
|
|
|
icon="ArrowRight"
|
|
|
|
|
iconPosition="right"
|
|
|
|
|
loading={isSubmitting}
|
|
|
|
|
disabled={isSubmitting}
|
2026-11-16 21:58:18 +06:00
|
|
|
className="mt-0.5 w-full justify-center"
|
2026-10-23 19:56:38 -07:00
|
|
|
>
|
|
|
|
|
Sign in
|
|
|
|
|
</Button>
|
|
|
|
|
|
2026-11-17 20:05:00 +06:00
|
|
|
{showDemoHint && (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setEmail('admin@datastack.dev');
|
|
|
|
|
setPassword('admin12345678');
|
|
|
|
|
}}
|
|
|
|
|
className="mt-0.5 text-center font-mono text-[10.5px] text-[var(--accent)] hover:underline cursor-pointer"
|
|
|
|
|
>
|
|
|
|
|
admin@datastack.dev / admin12345678
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
2026-10-23 19:56:38 -07:00
|
|
|
</form>
|
|
|
|
|
|
2026-11-16 21:58:18 +06:00
|
|
|
<footer className="flex items-center justify-end border-t border-[var(--border)] px-4 py-2 text-[10.5px]">
|
2026-10-23 19:56:38 -07:00
|
|
|
<Link href="/" className="text-[var(--text-muted)] hover:text-[var(--text)]">Home <ArrowRight className="inline h-3 w-3" aria-hidden="true" /></Link>
|
|
|
|
|
</footer>
|
|
|
|
|
</section>
|
|
|
|
|
</main>
|
|
|
|
|
);
|
|
|
|
|
}
|