feat(auth): add support for cross-subdomain cookies and improve environment variable handling

This commit is contained in:
2026-03-11 23:39:14 +08:00
parent 20e0cd068f
commit 103c86e14d
2 changed files with 20 additions and 2 deletions

View File

@@ -5,6 +5,9 @@ import * as schema from "@/db/schema.ts";
import { admin, bearer, username } from "better-auth/plugins";
import { passkey } from "@better-auth/passkey";
const webOrigin = process.env.WEB_ORIGIN ?? "http://localhost:3000";
const rpID = new URL(webOrigin).hostname;
export const auth = betterAuth({
database: drizzleAdapter(useDrizzle(), {
provider: "pg",
@@ -12,7 +15,7 @@ export const auth = betterAuth({
...schema,
},
}),
trustedOrigins: [process.env.WEB_ORIGIN ?? "http://localhost:3000"],
trustedOrigins: [webOrigin],
appName: "Helios EVCS",
user: {
additionalFields: {},
@@ -20,8 +23,20 @@ export const auth = betterAuth({
emailAndPassword: {
enabled: true,
},
plugins: [admin(), username(), bearer(), passkey()],
plugins: [
admin(),
username(),
bearer(),
passkey({
rpID,
rpName: "Helios EVCS",
origin: webOrigin,
}),
],
advanced: {
cookiePrefix: "helios_auth",
crossSubdomainCookies: process.env.COOKIE_DOMAIN
? { enabled: true, domain: process.env.COOKIE_DOMAIN }
: { enabled: false },
},
});