import { thisWebURL } from "https://esm.town/v/stevekrouse/thisWebURL?v=2";
import { ValTownAdapter } from "https://esm.town/v/yawnxyz/lucia_adapter_valtown";
import { createUser, getUser, verifyPassword } from "https://esm.town/v/yawnxyz/lucia_sqlite";
import { Hono } from "npm:hono";
import { getCookie, setCookie } from "npm:hono/cookie";
import { HTTPException } from "npm:hono/http-exception";
import { jsxRenderer } from "npm:hono/jsx-renderer";
import { Lucia, Session, User, verifyRequestOrigin } from "npm:lucia@3.0.1";
import { createDate, isWithinExpirationDate, TimeSpan } from "npm:oslo";
const userTable = "lucia_users_1";
const sessionTable = "lucia_sessions_1";
const adapter = new ValTownAdapter({ userTable, sessionTable });
import { generateId } from "npm:lucia@3.0.1";
import { generateState, OAuth2RequestError, generateCodeVerifier, GitHub, Google } from "npm:arctic";
import { serializeCookie, parseCookies } from "npm:oslo/cookie";
const githubClientId = Deno.env.get("GITHUB_OAUTH_COVERSHEET_CLIENT");
const githubClientSecret = Deno.env.get("GITHUB_OAUTH_COVERSHEET_SECRET");
export const github = new GitHub(githubClientId, githubClientSecret);
const googleClientId = Deno.env.get("GOOGLE_OAUTH_COVERSHEET_CLIENT");
const googleClientSecret = Deno.env.get("GOOGLE_OAUTH_COVERSHEET_SECRET");
const google = new Google(googleClientId, googleClientSecret, `https://yawnxyz-hncloneluciaoauth.web.val.run/auth/google/callback`);
export const lucia = new Lucia(adapter, {
sessionCookie: {
attributes: {
sameSite: "none",
},
},
getUserAttributes: (attributes) => {
return {
github_id: attributes.github_id,
username: attributes.username,
};
},
});
declare module "npm:lucia" {
interface Register {
Lucia: typeof lucia;
DatabaseUserAttributes: DatabaseUserAttributes;
}
}
interface DatabaseUserAttributes {
github_id: number;
username: string;
}
interface GitHubUserResult {
id: number;
login: string;
}
export const luciaMiddleware = (handler: (req: Request) => Response | Promise<Response>) => {
const app = new Hono<{
Variables: {
user: User | null;
session: Session | null;
};
}>();
app.use("*", async (c, next) => {
const sessionId = getCookie(c, lucia.sessionCookieName) ?? null;
if (!sessionId) {
c.set("user", null);
c.set("session", null);
return next();
}
const { session, user } = await lucia.validateSession(sessionId);
if (session && session.fresh) {
c.header("Set-Cookie", lucia.createSessionCookie(session.id).serialize(), {
append: true,
});
}
if (!session) {
c.header("Set-Cookie", lucia.createBlankSessionCookie().serialize(), {
append: true,
});
}
c.set("user", user);
c.set("session", session);
return next();
});
app.get("/auth/signup", c =>
c.render(
<div>
<h1 style="text-align: center;" > Sign up </h1>
< form
method="post"
action="/auth/signup"