scaffold auth

main
Ethan Niser 2023-09-14 19:43:27 +00:00 committed by GitHub
parent f7355f8576
commit 1acd519951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 69 additions and 40 deletions

Binary file not shown.

@ -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"
}

@ -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;

@ -14,34 +14,6 @@ export const BaseHtml = ({ children }: Html.PropsWithChildren) => (
href="https://cdn.jsdelivr.net/npm/@unocss/reset/tailwind.min.css"
/>
<link rel="stylesheet" href="/public/dist/unocss.css" />
<script>
{`
(function () {
var consoleOutput = document.getElementById('consoleOutput');
if (!window.console) {
window.console = {};
}
var oldConsoleLog = console.log;
console.log = function (message) {
// Append the log message to the consoleOutput div
if (consoleOutput) {
var logMessage = document.createElement('p');
logMessage.textContent = message;
consoleOutput.appendChild(logMessage);
}
// Call the original console.log function
oldConsoleLog.apply(console, arguments);
};
// You can do the same for other console methods like error, warn, etc. if needed
})()
`}
</script>
<script>
{`
(function () {
@ -67,13 +39,6 @@ export const BaseHtml = ({ children }: Html.PropsWithChildren) => (
`}
</script>
</head>
<body>
<div
id="consoleOutput"
style="background-color: #f0f0f0; padding: 10px;"
></div>
<script>console.log("Hello from the client side!");</script>
{children}
</body>
<body>{children}</body>
</html>
);

@ -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 = {

@ -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,
});

@ -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"),
});

@ -1 +1,2 @@
export { todos } from "./todos";
export * from "./auth"

@ -27,7 +27,7 @@ type RouterPattern<T extends string> =
: T;
declare namespace JSX {
type Schema = import("./main").App["meta"]["schema"];
type Schema = import("../main").App["meta"]["schema"];
type PostRoutes = RoutesByType<Schema, "post">;
type GetRoutes = RoutesByType<Schema, "get">;

@ -0,0 +1,6 @@
/// <reference types="lucia" />
declare namespace Lucia {
type Auth = import("../auth/index").Auth;
type DatabaseUserAttributes = {};
type DatabaseSessionAttributes = {};
}