mirror of
https://github.com/OpenCut-app/OpenCut.git
synced 2026-07-13 21:52:53 +02:00
feat: integrate @t3-oss/env for environment variable management across the application
This commit is contained in:
@@ -7,13 +7,16 @@
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./client": "./src/client.ts",
|
||||
"./server": "./src/server.ts"
|
||||
"./server": "./src/server.ts",
|
||||
"./keys": "./src/keys.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@opencut/db": "workspace:*",
|
||||
"@t3-oss/env-nextjs": "^0.13.8",
|
||||
"better-auth": "^1.1.1",
|
||||
"@opencut/db": "workspace:*"
|
||||
"zod": "^4.0.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.2"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
import { createAuthClient } from "better-auth/react";
|
||||
import { keys } from "./keys";
|
||||
|
||||
const { NEXT_PUBLIC_BETTER_AUTH_URL } = keys();
|
||||
|
||||
export const { signIn, signUp, useSession } = createAuthClient({
|
||||
baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL!,
|
||||
});
|
||||
baseURL: NEXT_PUBLIC_BETTER_AUTH_URL,
|
||||
});
|
||||
|
||||
@@ -2,4 +2,7 @@
|
||||
export * from "./server";
|
||||
|
||||
// Re-export client auth
|
||||
export * from "./client";
|
||||
export * from "./client";
|
||||
|
||||
// Re-export keys
|
||||
export * from "./keys";
|
||||
@@ -0,0 +1,20 @@
|
||||
import { createEnv } from "@t3-oss/env-nextjs";
|
||||
import { z } from "zod";
|
||||
|
||||
export const keys = () =>
|
||||
createEnv({
|
||||
server: {
|
||||
BETTER_AUTH_SECRET: z.string(),
|
||||
GOOGLE_CLIENT_ID: z.string(),
|
||||
GOOGLE_CLIENT_SECRET: z.string(),
|
||||
},
|
||||
client: {
|
||||
NEXT_PUBLIC_BETTER_AUTH_URL: z.string().url(),
|
||||
},
|
||||
runtimeEnv: {
|
||||
NEXT_PUBLIC_BETTER_AUTH_URL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL,
|
||||
BETTER_AUTH_SECRET: process.env.BETTER_AUTH_SECRET,
|
||||
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
|
||||
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,
|
||||
},
|
||||
});
|
||||
@@ -1,13 +1,16 @@
|
||||
import { betterAuth } from "better-auth";
|
||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||
import { db } from "@opencut/db";
|
||||
import { keys } from "./keys";
|
||||
|
||||
const { NEXT_PUBLIC_BETTER_AUTH_URL, BETTER_AUTH_SECRET, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET } = keys()
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: drizzleAdapter(db, {
|
||||
provider: "pg",
|
||||
usePlural: true,
|
||||
}),
|
||||
secret: process.env.BETTER_AUTH_SECRET,
|
||||
secret: BETTER_AUTH_SECRET,
|
||||
user: {
|
||||
deleteUser: {
|
||||
enabled: true,
|
||||
@@ -18,11 +21,11 @@ export const auth = betterAuth({
|
||||
},
|
||||
socialProviders: {
|
||||
google: {
|
||||
clientId: process.env.GOOGLE_CLIENT_ID as string,
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
|
||||
clientId: GOOGLE_CLIENT_ID,
|
||||
clientSecret: GOOGLE_CLIENT_SECRET,
|
||||
},
|
||||
},
|
||||
baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL,
|
||||
baseURL: NEXT_PUBLIC_BETTER_AUTH_URL,
|
||||
appName: "OpenCut",
|
||||
trustedOrigins: ["http://localhost:3000"],
|
||||
});
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
import type { Config } from "drizzle-kit";
|
||||
import * as dotenv from "dotenv";
|
||||
import { keys } from "./src/keys";
|
||||
|
||||
const { NODE_ENV, DATABASE_URL } = keys();
|
||||
|
||||
// Load the right env file based on environment
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
if (NODE_ENV === "production") {
|
||||
dotenv.config({ path: ".env.production" });
|
||||
} else {
|
||||
dotenv.config({ path: ".env.local" });
|
||||
}
|
||||
|
||||
if (!process.env.DATABASE_URL) {
|
||||
throw new Error("DATABASE_URL is not set");
|
||||
}
|
||||
|
||||
export default {
|
||||
schema: "./src/schema.ts",
|
||||
dialect: "postgresql",
|
||||
@@ -19,8 +18,8 @@ export default {
|
||||
table: "drizzle_migrations",
|
||||
},
|
||||
dbCredentials: {
|
||||
url: process.env.DATABASE_URL,
|
||||
url: DATABASE_URL,
|
||||
},
|
||||
out: "./migrations",
|
||||
strict: process.env.NODE_ENV === "production",
|
||||
} satisfies Config;
|
||||
strict: NODE_ENV === "production",
|
||||
} satisfies Config;
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./schema": "./src/schema.ts",
|
||||
"./drizzle.config": "./drizzle.config.ts"
|
||||
"./drizzle.config": "./drizzle.config.ts",
|
||||
"./keys": "./src/keys.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"db:generate": "drizzle-kit generate",
|
||||
@@ -16,8 +17,10 @@
|
||||
"db:studio": "drizzle-kit studio"
|
||||
},
|
||||
"dependencies": {
|
||||
"@t3-oss/env-nextjs": "^0.13.8",
|
||||
"drizzle-orm": "^0.44.2",
|
||||
"postgres": "^3.4.5"
|
||||
"postgres": "^3.4.5",
|
||||
"zod": "^4.0.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"drizzle-kit": "^0.31.1",
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
import * as schema from "./schema";
|
||||
import { keys } from "./keys";
|
||||
|
||||
const { DATABASE_URL } = keys();
|
||||
// Create a lazy database instance that only initializes when accessed
|
||||
let _db: ReturnType<typeof drizzle> | null = null;
|
||||
|
||||
function getDb() {
|
||||
if (!process.env.DATABASE_URL) {
|
||||
throw new Error("DATABASE_URL is not set");
|
||||
}
|
||||
|
||||
if (!_db) {
|
||||
const client = postgres(process.env.DATABASE_URL);
|
||||
const client = postgres(DATABASE_URL);
|
||||
_db = drizzle(client, { schema });
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { createEnv } from "@t3-oss/env-nextjs";
|
||||
import { z } from "zod";
|
||||
|
||||
export const keys = () =>
|
||||
createEnv({
|
||||
server: {
|
||||
NODE_ENV: z
|
||||
.enum(["development", "production", "test"])
|
||||
.default("development"),
|
||||
DATABASE_URL: z
|
||||
.string()
|
||||
.startsWith("postgres://")
|
||||
.or(z.string().startsWith("postgresql://")),
|
||||
},
|
||||
runtimeEnv: {
|
||||
NODE_ENV: process.env.NODE_ENV,
|
||||
DATABASE_URL: process.env.DATABASE_URL,
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user