- Colour tokens in index.css named by role (ink, body, muted, link, brand, action, surface, tint, danger), used everywhere instead of hex - Dark mode: follows the system, a header toggle (site and admin) overrides it and is remembered; set before first paint in index.html - Data colours go through src/lib/color.ts: withAlpha for tints, accentText (lighter on dark) for text in an accent, readableOn for text on one, so an admin's pale colour still reads - Migration 040 drops events.gradient: a card is its photo or plain, with its colour as the border; other decorative gradients are solid - The countdown and a hero event without a photo mix their colour toward ink behind white text Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
3 KiB
TypeScript
98 lines
3 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
AUTH CONTEXT
|
|
|
|
One fetch of /api/auth/me at mount decides whether anyone is
|
|
signed in. There's no token to store: the session cookie is
|
|
HttpOnly, so this code can't read it and neither can anything
|
|
injected into the page. "Am I signed in" is always a question
|
|
for the server.
|
|
|
|
RequireAuth guards routes. Worth being clear about what that
|
|
does and doesn't do: it hides the interface, not the data. The
|
|
protection is the 401 from /api/admin, this only stops someone
|
|
staring at an empty table.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
import { createContext, useCallback, useContext, useEffect, useState } from "react";
|
|
import { Navigate, Outlet, useLocation } from "react-router-dom";
|
|
|
|
import { get, post, ApiError } from "./api.ts";
|
|
|
|
const AuthContext = createContext<any>(null);
|
|
|
|
export function AuthProvider({ children }) {
|
|
const [user, setUser] = useState(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
let ignore = false;
|
|
|
|
get("/auth/me", { ttl: 0 })
|
|
.then((data) => {
|
|
if (!ignore) setUser(data.user);
|
|
})
|
|
.catch(() => {
|
|
if (!ignore) setUser(null); // 401 is the normal case
|
|
})
|
|
.finally(() => {
|
|
if (!ignore) setLoading(false);
|
|
});
|
|
|
|
return () => {
|
|
ignore = true;
|
|
};
|
|
}, []);
|
|
|
|
const login = useCallback(async (email, password) => {
|
|
const data = await post("/auth/login", { email, password });
|
|
setUser(data.user);
|
|
return data.user;
|
|
}, []);
|
|
|
|
const logout = useCallback(async () => {
|
|
try {
|
|
await post("/auth/logout", {});
|
|
} finally {
|
|
// Whatever the server said, this browser is done.
|
|
setUser(null);
|
|
}
|
|
}, []);
|
|
|
|
return (
|
|
<AuthContext.Provider value={{ user, loading, login, logout }}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useAuth() {
|
|
const value = useContext(AuthContext);
|
|
if (!value) throw new Error("useAuth used outside AuthProvider");
|
|
return value;
|
|
}
|
|
|
|
/* Signals a session that ended while the page was open, the
|
|
admin pages call this when a request comes back 401. */
|
|
export function isUnauthorized(error) {
|
|
return error instanceof ApiError && error.status === 401;
|
|
}
|
|
|
|
export function RequireAuth() {
|
|
const { user, loading } = useAuth();
|
|
const location = useLocation();
|
|
|
|
if (loading) {
|
|
return (
|
|
<div className="flex min-h-screen items-center justify-center text-muted">
|
|
Checking your session…
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!user) {
|
|
// `from` lets the login page send them back where they aimed.
|
|
return <Navigate to="/admin/login" state={{ from: location }} replace />;
|
|
}
|
|
|
|
return <Outlet />;
|
|
}
|