import { Hono } from "hono";
import * as swa from "@simplewebauthn/server";
import { randomUUID } from "node:crypto";
import { and, eq, gt, sql } from "drizzle-orm";
import { RP_ID, ORIGIN, db } from "./index.js";
import { sessionTable, userTable, webauthnChallenges } from "./db/schema.js";
import { stringify, parse } from "superjson";
import { getCookie, setCookie } from "hono/cookie";
import type { Context } from "hono";
export const LoginForm = () =>
Could not verify registration response!
); await db.insert(userTable).values({ name: username, passkey: stringify(r.registrationInfo), passkeyId: r.registrationInfo!.credential.id }); return c.html(You now have an account!
); }); app.post("/login-begin", async c => { let options = await swa.generateAuthenticationOptions({ rpID: RP_ID, userVerification: "preferred", }); let key = randomUUID(); await db.insert(webauthnChallenges).values({ challenge: options.challenge, key: "login:" + key }); return c.html( ); }); app.post("/login-finish", async c => { let { resp, key } = await c.req.json(); let [{ chall }] = await db.delete(webauthnChallenges).where(eq(webauthnChallenges.key, "login:" + key)).returning({ chall: webauthnChallenges.challenge }); let [{ id: userId, passkey }] = await db.select().from(userTable).where(user => eq(user.passkeyId, resp.id)); if (!passkey) return c.html(Who are you?
); let r = await swa.verifyAuthenticationResponse({ response: resp, expectedChallenge: chall, expectedOrigin: ORIGIN, expectedRPID: RP_ID, requireUserVerification: false, credential: parseCould not verify authentication response!
); let uuid = randomUUID(); await db.insert(sessionTable).values({ userId, uuid }); setCookie(c, "session", uuid); return c.html(Logged in!
); });