Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat(auth): enhance base URL handling and trusted origins

Andrej Acevski07fa1b69b20d
diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.ts
index c60c09342..d5444b253 100644
--- a/apps/api/src/auth.ts
+++ b/apps/api/src/auth.ts
@@ -34,9 +34,27 @@ const isCrossSubdomain = (() => {
}
})();
+const trustedOrigins = [clientUrl];
+try {
+ const apiOrigin = new URL(apiUrl);
+ const apiOriginString = `${apiOrigin.protocol}//${apiOrigin.host}`;
+ if (!trustedOrigins.includes(apiOriginString)) {
+ trustedOrigins.push(apiOriginString);
+ }
+} catch {}
+
+const baseURLWithoutPath = (() => {
+ try {
+ const url = new URL(apiUrl);
+ return `${url.protocol}//${url.host}`;
+ } catch {
+ return apiUrl.split("/").slice(0, 3).join("/"); // Get protocol://host
+ }
+})();
+
export const auth = betterAuth({
- baseURL: apiUrl,
- trustedOrigins: [clientUrl],
+ baseURL: baseURLWithoutPath,
+ trustedOrigins,
secret: process.env.AUTH_SECRET || "",
basePath: "/api/auth",
database: drizzleAdapter(db, {
diff --git a/apps/web/src/lib/auth-client.ts b/apps/web/src/lib/auth-client.ts
index 0f866626e..e8dbb86a9 100644
--- a/apps/web/src/lib/auth-client.ts
+++ b/apps/web/src/lib/auth-client.ts
@@ -7,8 +7,18 @@ import {
import { createAuthClient } from "better-auth/react";
import { ac, admin, member, owner } from "./permissions";
+const getBaseURL = () => {
+ const apiUrl = import.meta.env.VITE_API_URL || "http://localhost:1337";
+ try {
+ const url = new URL(apiUrl);
+ return `${url.protocol}//${url.host}`;
+ } catch {
+ return apiUrl.split("/").slice(0, 3).join("/");
+ }
+};
+
export const authClient = createAuthClient({
- baseURL: import.meta.env.VITE_API_URL || "http://localhost:1337",
+ baseURL: getBaseURL(),
basePath: "/api/auth",
plugins: [
anonymousClient(),