import crypto from "node:crypto"; import { Hono } from "hono"; import { db } from "../db/index.js"; import { users } from "../db/schema.js"; import { createSession, type AuthEnv } from "./session.js"; const APP_URL = process.env.APP_URL ?? "http://localhost:5173"; // Deterministic *negative* id so impersonated identities can never collide // with real GitHub account ids. Also used by the seed script. export function devGithubId(username: string): number { const hex = crypto.createHash("sha1").update(username).digest("hex").slice(0, 8); return -Number.parseInt(hex, 16); } // Dev-only identity impersonation: GET /auth/dev/ada signs you in as "ada", // creating the user if needed. Hard-disabled in production. const dev = new Hono(); dev.get("/dev/:username", async (c) => { if (process.env.NODE_ENV === "production") return c.notFound(); const username = c.req.param("username").toLowerCase().replace(/[^a-z0-9-]/g, ""); if (!username) return c.json({ error: "Invalid username" }, 400); const [user] = await db .insert(users) .values({ githubId: devGithubId(username), username, avatarUrl: null }) .onConflictDoUpdate({ target: users.githubId, set: { username } }) .returning(); await createSession(c, user.id); return c.redirect(APP_URL); }); export default dev;