diff --git a/bun.lockb b/bun.lockb index 66641e4..13ce365 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index b99c3e9..e9054d9 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "module": "src/main.ts", "type": "module", "scripts": { - "dev": "concurrently \"bun run --hot src/main.ts\" \"bun run uno:dev\" \"bun run liveReload\"", + "dev": "concurrently \"bun run --hot src/main.ts\" \"bun run uno:dev\"", "liveReload": "bun run --hot src/dev/liveReload.ts", "start": "bun run uno && bun run src/main.ts", "db:push": "bunx drizzle-kit push:sqlite", @@ -27,11 +27,13 @@ "@elysiajs/swagger": "^0.6.2", "@kitajs/html": "^2.1.2", "@libsql/client": "^0.3.4", + "@lucia-auth/adapter-sqlite": "^2.0.0", "@t3-oss/env-core": "^0.6.1", "chokidar": "^3.5.3", "drizzle-orm": "^0.28.6", "drizzle-typebox": "^0.1.1", "elysia": "^0.6.22", + "lucia": "^2.6.0", "pino-pretty": "^10.2.0", "zod": "^3.22.2" } diff --git a/src/auth/index.ts b/src/auth/index.ts new file mode 100644 index 0000000..d16052c --- /dev/null +++ b/src/auth/index.ts @@ -0,0 +1,27 @@ +import { lucia } from "lucia"; +import { web } from "lucia/middleware"; +import { libsql } from "@lucia-auth/adapter-sqlite"; +import { config } from "../config"; +import { client } from "../db"; + +const envAliasMap = { + production: "PROD", + development: "DEV", +} as const; + +const envAlias = envAliasMap[config.env.NODE_ENV]; + +export const auth = lucia({ + env: envAlias, + middleware: web(), + sessionCookie: { + expires: false, + }, + adapter: libsql(client, { + user: "user", + key: "user_key", + session: "user_session", + }), +}); + +export type Auth = typeof auth; diff --git a/src/components/base.tsx b/src/components/base.tsx index 34288b4..7d29839 100644 --- a/src/components/base.tsx +++ b/src/components/base.tsx @@ -14,34 +14,6 @@ export const BaseHtml = ({ children }: Html.PropsWithChildren) => ( href="https://cdn.jsdelivr.net/npm/@unocss/reset/tailwind.min.css" /> - - -
- - {children} - + {children} ); diff --git a/src/config/index.ts b/src/config/index.ts index 3662bfd..09d30b5 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -6,13 +6,14 @@ const env = createEnv({ LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]), DATABASE_URL: z.string().min(1), DATABASE_AUTH_TOKEN: z.string().min(1), + NODE_ENV: z.enum(["development", "production"]), }, runtimeEnv: process.env, }); const args = { // watch: process.argv.includes("--watch"), - liveReload: true + // liveReload: true, }; export const config = { diff --git a/src/db/index.ts b/src/db/index.ts index e76edff..cb5631a 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -3,7 +3,7 @@ import { createClient } from "@libsql/client"; import * as schema from "./schemas"; import { config } from "../config"; -const client = createClient({ +export const client = createClient({ url: config.env.DATABASE_URL, authToken: config.env.DATABASE_AUTH_TOKEN, }); diff --git a/src/db/schemas/auth.ts b/src/db/schemas/auth.ts new file mode 100644 index 0000000..254e67d --- /dev/null +++ b/src/db/schemas/auth.ts @@ -0,0 +1,27 @@ +import { sqliteTable, text, blob } from "drizzle-orm/sqlite-core"; + +export const user = sqliteTable("user", { + id: text("id").primaryKey(), + // other user attributes +}); + +export const session = sqliteTable("user_session", { + id: text("id").primaryKey(), + userId: text("user_id") + .notNull() + .references(() => user.id), + activeExpires: blob("active_expires", { + mode: "bigint", + }).notNull(), + idleExpires: blob("idle_expires", { + mode: "bigint", + }).notNull(), +}); + +export const key = sqliteTable("user_key", { + id: text("id").primaryKey(), + userId: text("user_id") + .notNull() + .references(() => user.id), + hashedPassword: text("hashed_password"), +}); diff --git a/src/db/schemas/index.ts b/src/db/schemas/index.ts index c97aa6b..8bfc8b1 100644 --- a/src/db/schemas/index.ts +++ b/src/db/schemas/index.ts @@ -1 +1,2 @@ export { todos } from "./todos"; +export * from "./auth" \ No newline at end of file diff --git a/src/htmx.d.ts b/src/types/htmx.d.ts similarity index 95% rename from src/htmx.d.ts rename to src/types/htmx.d.ts index 52105a5..bfe856a 100644 --- a/src/htmx.d.ts +++ b/src/types/htmx.d.ts @@ -27,7 +27,7 @@ type RouterPattern = : T; declare namespace JSX { - type Schema = import("./main").App["meta"]["schema"]; + type Schema = import("../main").App["meta"]["schema"]; type PostRoutes = RoutesByType; type GetRoutes = RoutesByType; diff --git a/src/types/lucia.d.ts b/src/types/lucia.d.ts new file mode 100644 index 0000000..481dd47 --- /dev/null +++ b/src/types/lucia.d.ts @@ -0,0 +1,6 @@ +/// +declare namespace Lucia { + type Auth = import("../auth/index").Auth; + type DatabaseUserAttributes = {}; + type DatabaseSessionAttributes = {}; +}