finished setting up server + added docker compose

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-26 23:06:06 -05:00
parent a6848fb7e9
commit 610c9606dc
13 changed files with 684 additions and 30 deletions
+58
View File
@@ -0,0 +1,58 @@
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { admin, haveIBeenPwned } from "better-auth/plugins";
import * as schema from "@/lib/db/schema/auth";
import { passkey } from "@better-auth/passkey";
import { betterAuth } from "better-auth";
import { Context } from "elysia";
import { db } from "@/lib/db";
const rpID = process.env.BETTER_AUTH_URL?.replace(/^https?:\/\//, "") || "localhost";
const resolvedLogLevel = process.env.BETTER_AUTH_LOG_LEVEL ?? (process.env.NODE_ENV === "production" ? "info" : "debug");
const logLevel: "error" | "debug" | "info" | "warn" =
resolvedLogLevel === "error" || resolvedLogLevel === "warn" || resolvedLogLevel === "info" || resolvedLogLevel === "debug"
? resolvedLogLevel
: "info";
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema,
}),
advanced: {
cookiePrefix: "turborepo-starter-project",
},
emailAndPassword: {
enabled: true,
maxPasswordLength: 128,
minPasswordLength: 8,
},
logger: {
level: logLevel,
},
session: {
expiresIn: 60 * 60 * 24, // 1 day in seconds
},
plugins: [
admin({
bannedUserMessage: "Your account has been banned. Please contact support for more information.",
impersonationSessionDuration: 60 * 60, // 1 hour in seconds
}),
haveIBeenPwned({
customPasswordCompromisedMessage: "This password has been compromised in a data breach. Please choose a different password.",
}),
passkey({
rpName: "Turborepo Starter Project",
rpID,
authenticatorSelection: {
residentKey: "required",
userVerification: "required",
authenticatorAttachment: "platform",
},
advanced: {
webAuthnChallengeCookie: "turborepo-starter-project",
}
})
],
basePath: "/auth",
});
+17
View File
@@ -0,0 +1,17 @@
import "dotenv/config";
const isBun = typeof Bun !== "undefined";
export const db = await (async () => {
if (isBun) {
const { SQL } = await import("bun");
const { drizzle } = await import("drizzle-orm/bun-sql");
return drizzle(new SQL(process.env.DATABASE_URL!));
} else {
const { default: postgres } = await import("postgres");
const { drizzle } = await import("drizzle-orm/postgres-js");
return drizzle(postgres(process.env.DATABASE_URL!));
}
})();
export * from "./schema";
+136
View File
@@ -0,0 +1,136 @@
import { relations } from "drizzle-orm";
import {
pgTable,
text,
timestamp,
boolean,
integer,
index,
} from "drizzle-orm/pg-core";
export const user = pgTable("user", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull().unique(),
emailVerified: boolean("email_verified").default(false).notNull(),
image: text("image"),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.defaultNow()
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
role: text("role"),
banned: boolean("banned").default(false),
banReason: text("ban_reason"),
banExpires: timestamp("ban_expires"),
});
export const session = pgTable(
"session",
{
id: text("id").primaryKey(),
expiresAt: timestamp("expires_at").notNull(),
token: text("token").notNull().unique(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
ipAddress: text("ip_address"),
userAgent: text("user_agent"),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
impersonatedBy: text("impersonated_by"),
},
(table) => [index("session_userId_idx").on(table.userId)],
);
export const account = pgTable(
"account",
{
id: text("id").primaryKey(),
accountId: text("account_id").notNull(),
providerId: text("provider_id").notNull(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
accessToken: text("access_token"),
refreshToken: text("refresh_token"),
idToken: text("id_token"),
accessTokenExpiresAt: timestamp("access_token_expires_at"),
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
scope: text("scope"),
password: text("password"),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
},
(table) => [index("account_userId_idx").on(table.userId)],
);
export const verification = pgTable(
"verification",
{
id: text("id").primaryKey(),
identifier: text("identifier").notNull(),
value: text("value").notNull(),
expiresAt: timestamp("expires_at").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.defaultNow()
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
},
(table) => [index("verification_identifier_idx").on(table.identifier)],
);
export const passkey = pgTable(
"passkey",
{
id: text("id").primaryKey(),
name: text("name"),
publicKey: text("public_key").notNull(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
credentialID: text("credential_id").notNull(),
counter: integer("counter").notNull(),
deviceType: text("device_type").notNull(),
backedUp: boolean("backed_up").notNull(),
transports: text("transports"),
createdAt: timestamp("created_at"),
aaguid: text("aaguid"),
},
(table) => [
index("passkey_userId_idx").on(table.userId),
index("passkey_credentialID_idx").on(table.credentialID),
],
);
export const userRelations = relations(user, ({ many }) => ({
sessions: many(session),
accounts: many(account),
passkeys: many(passkey),
}));
export const sessionRelations = relations(session, ({ one }) => ({
user: one(user, {
fields: [session.userId],
references: [user.id],
}),
}));
export const accountRelations = relations(account, ({ one }) => ({
user: one(user, {
fields: [account.userId],
references: [user.id],
}),
}));
export const passkeyRelations = relations(passkey, ({ one }) => ({
user: one(user, {
fields: [passkey.userId],
references: [user.id],
}),
}));
+1
View File
@@ -0,0 +1 @@
export * from "./auth";
+39
View File
@@ -0,0 +1,39 @@
import { RedisClient } from "bun";
type RedisOptions = {
url: string;
options?: Bun.RedisOptions;
};
class Redis {
private client: RedisClient;
constructor(options: RedisOptions) {
this.client = new RedisClient(options.url, options.options);
}
async set(key: string, value: string, expireInSeconds?: number) {
if (expireInSeconds) {
await this.client.set(key, value, "EX", expireInSeconds);
} else {
await this.client.set(key, value);
}
}
async get<T>(key: string): Promise<T | null> {
const value = await this.client.get(key);
return value ? (JSON.parse(value) as T) : null;
}
async del(key: string) {
await this.client.del(key);
}
async exists(key: string): Promise<boolean> {
return await this.client.exists(key);
}
}
export const redis = new Redis({
url: process.env.REDIS_URL || "redis://localhost:6379",
});
+39
View File
@@ -0,0 +1,39 @@
import { S3Client } from "bun";
interface S3Options {
accessKeyId: string
secretAccessKey: string
bucket: string
endpoint: string
}
class S3 {
private client: S3Client;
constructor(options: S3Options) {
this.client = new S3Client(options);
}
async upload(key: string, body: File) {
return await this.client.write(key, body);
}
async get(key: string) {
return await this.client.file(key);
}
async delete(key: string) {
return await this.client.delete(key);
}
async presignedUrl(key: string, expiresIn: number = 3600) {
return await this.client.presign(key, { expiresIn });
}
}
export const s3 = new S3({
accessKeyId: process.env.S3_ACCESS_KEY_ID!,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
bucket: process.env.S3_BUCKET!,
endpoint: process.env.S3_ENDPOINT!,
});