feat(dashboard-v2): sign in on 401 via login template page

This commit is contained in:
sam committed 2026-10-26 23:52:12 +11:00
1 parent 5fefd1902f
commit dc1e45ff82
4 files changed
+136 -4

No files matched your search

+10 -2
View File
@@ -1,6 +1,14 @@
import { useCallback, useState } from "react";
import LoginPage from "./pages/LoginPage.tsx";
import StatusPage from "./pages/StatusPage.tsx";
// One page so far, so no shell or router yet: they land with the second page.
export default function App() {
return <StatusPage />;
const [needsAuth, setNeedsAuth] = useState(false);
const showLogin = useCallback(() => setNeedsAuth(true), []);
const showStatus = useCallback(() => setNeedsAuth(false), []);
// The status page remounts after sign-in, so it loads with the session.
if (needsAuth) return <LoginPage onSignedIn={showStatus} />;
return <StatusPage onAuthRequired={showLogin} />;
}
+5
View File
@@ -92,4 +92,9 @@ export interface ComponentVersion {
export const api = {
services: () => request<Service[]>("/api/services"),
version: () => request<{ components: ComponentVersion[] }>("/api/version"),
signIn: (email: string, password: string) =>
request<unknown>("/api/auth/login", {
method: "POST",
body: { email, password },
}),
};
+110
View File
@@ -0,0 +1,110 @@
import { useState, type CSSProperties } from "react";
import { Banner } from "@astryxdesign/core/Banner";
import { Button } from "@astryxdesign/core/Button";
import { Card } from "@astryxdesign/core/Card";
import { Center } from "@astryxdesign/core/Center";
import { Icon } from "@astryxdesign/core/Icon";
import { VStack } from "@astryxdesign/core/Layout";
import { Heading, Text } from "@astryxdesign/core/Text";
import { TextInput } from "@astryxdesign/core/TextInput";
import { CubeIcon } from "@heroicons/react/24/outline";
import { api } from "../lib/api.ts";
// Standalone auth page paints its own body background (no host shell).
const pageStyle: CSSProperties = {
minHeight: "100%",
backgroundColor: "var(--color-background-body)",
};
// Cap the column at 400px but let it shrink to fit narrow screens (Stack
// has no maxWidth prop, so it's set here).
const contentStyle: CSSProperties = {
width: "100%",
maxWidth: 400,
};
export default function LoginPage({
onSignedIn,
}: {
onSignedIn: () => void;
}) {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState("");
const handleSignIn = async () => {
setError("");
if (!email || !password) {
setError("Enter both email and password.");
return;
}
setIsLoading(true);
try {
await api.signIn(email, password);
onSignedIn();
} catch (err) {
setError(err instanceof Error ? err.message : "Sign-in failed");
} finally {
setIsLoading(false);
}
};
return (
<Center axis="both" padding={6} style={pageStyle}>
<VStack gap={4} hAlign="center" style={contentStyle}>
{/* Logo */}
<VStack gap={2} hAlign="center">
<Icon icon={CubeIcon} size="lg" />
<Text type="body" weight="bold" size="lg">
Archie
</Text>
</VStack>
{/* Card */}
<Card padding={8} width="100%">
<VStack gap={4} hAlign="stretch">
<VStack gap={1} hAlign="center">
<Heading level={2}>Sign in</Heading>
</VStack>
{error && <Banner status="error" title={error} container="card" />}
<TextInput
label="Email"
value={email}
onChange={setEmail}
onEnter={() => void handleSignIn()}
placeholder="you@example.com"
type="email"
size="lg"
autoComplete="email"
isDisabled={isLoading}
/>
<TextInput
label="Password"
value={password}
onChange={setPassword}
onEnter={() => void handleSignIn()}
placeholder="Enter your password"
type="password"
size="lg"
autoComplete="current-password"
isDisabled={isLoading}
/>
<Button
label="Sign in"
variant="primary"
size="lg"
isLoading={isLoading}
onClick={() => void handleSignIn()}
/>
</VStack>
</Card>
</VStack>
</Center>
);
}
+11 -2
View File
@@ -22,6 +22,7 @@ import { Heading, Text } from "@astryxdesign/core/Text";
import { ArrowPathIcon } from "@heroicons/react/24/outline";
import {
ApiError,
api,
type ComponentVersion,
type Service,
@@ -145,7 +146,11 @@ const versionColumns: TableColumn<VersionRow>[] = [
},
];
export default function StatusPage() {
export default function StatusPage({
onAuthRequired,
}: {
onAuthRequired: () => void;
}) {
const [services, setServices] = useState<Service[]>([]);
const [components, setComponents] = useState<ComponentVersion[]>([]);
const [loading, setLoading] = useState(true);
@@ -162,11 +167,15 @@ export default function StatusPage() {
setServices(fetchedServices);
setComponents(report.components);
} catch (err) {
if (err instanceof ApiError && err.status === 401) {
onAuthRequired();
return;
}
setError(err instanceof Error ? err.message : "Could not load status");
} finally {
setLoading(false);
}
}, []);
}, [onAuthRequired]);
useEffect(() => {
void load();