Compare commits

...

8 Commits

4 changed files with 41 additions and 19 deletions

View File

@@ -6,6 +6,8 @@ import { Elysia } from "elysia";
import api from "./api";
import { openInEditor } from "./utils/open-in-editor";
const PORT = process.env.PORT || 3000;
const isProduction = process.env.NODE_ENV === "production";
const app = new Elysia().use(api);
@@ -198,10 +200,11 @@ if (!isProduction) {
});
}
app.listen(3000);
app.listen(PORT);
console.log(
`🚀 Server running at http://localhost:3000 in ${isProduction ? "production" : "development"} mode`,
`🚀 Server running at http://localhost:${PORT} in ${isProduction ? "production" : "development"} mode`,
);
export type ApiApp = typeof app;

View File

@@ -2,11 +2,7 @@ import createClient from "openapi-fetch";
import type { paths } from "../../generated/api";
import { VITE_PUBLIC_URL } from "./env";
const baseUrl =
VITE_PUBLIC_URL ||
(typeof window !== "undefined"
? window.location.origin
: "http://localhost:3000");
const baseUrl = VITE_PUBLIC_URL;
export const apiClient = createClient<paths>({
baseUrl: baseUrl,

View File

@@ -1,16 +1,11 @@
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { PrismaClient } from "../../generated/prisma";
import logger from "./logger";
import { VITE_PUBLIC_URL } from "./env";
const baseUrl = process.env.VITE_PUBLIC_URL;
const baseUrl = VITE_PUBLIC_URL;
const prisma = new PrismaClient();
if (!baseUrl) {
logger.error("VITE_PUBLIC_URL is not defined");
throw new Error("VITE_PUBLIC_URL is not defined");
}
// logger.info('Initializing Better Auth with Prisma adapter');
export const auth = betterAuth({
baseURL: baseUrl,
@@ -37,8 +32,24 @@ export const auth = betterAuth({
},
},
},
databaseHooks: {
user: {
create: {
before: async (user) => {
if (user.email === process.env.ADMIN_EMAIL) {
return {
data: {
...user,
role: "admin",
},
};
}
return { data: user };
},
},
},
},
secret: process.env.BETTER_AUTH_SECRET,
trustedOrigins: ["http://localhost:5173", "http://localhost:3000"],
session: {
cookieCache: {
enabled: true,
@@ -48,5 +59,6 @@ export const auth = betterAuth({
},
advanced: {
cookiePrefix: "bun-react",
trustProxy: true,
},
});

View File

@@ -20,10 +20,21 @@ export const getEnv = (key: string, defaultValue = ""): string => {
return defaultValue;
};
export const VITE_PUBLIC_URL = getEnv(
"VITE_PUBLIC_URL",
"http://localhost:3000",
);
export const VITE_PUBLIC_URL = (() => {
// Priority:
// 1. BETTER_AUTH_URL (standard for better-auth)
// 2. VITE_PUBLIC_URL (our app standard)
// 3. window.location.origin (browser fallback)
const envUrl = getEnv("BETTER_AUTH_URL") || getEnv("VITE_PUBLIC_URL");
if (envUrl) return envUrl;
// Fallback for browser
if (typeof window !== "undefined") {
return window.location.origin;
}
return "http://localhost:3000";
})();
export const IS_DEV = (() => {
try {