import { App, staticFiles } from "fresh";
import { define, type State } from "./utils.ts";
import { getUserFromRequest } from "@/lib/auth.ts";
import {
devAutoLoginEnabled,
devSignedOut,
ensureDevAdmin,
} from "@/lib/dev-auth.ts";
export const app = new App<State>();
app.use(staticFiles());
app.use(define.middleware(async (ctx) => {
let user = await getUserFromRequest(ctx.req);
// Local dev server only: sign in as a seeded admin so the game and the
// administration screens are reachable without a login round-trip.
if (!user && devAutoLoginEnabled() && !devSignedOut(ctx.req)) {
user = await ensureDevAdmin();
}
ctx.state.user = user;
return await ctx.next();
}));
// Include file-system based routes here
app.fsRoutes();