feat(auth): add support for cross-subdomain cookies and improve environment variable handling
This commit is contained in:
@@ -1,2 +1,5 @@
|
|||||||
|
BETTER_AUTH_SECRET=
|
||||||
WEB_ORIGIN=http://localhost:3000
|
WEB_ORIGIN=http://localhost:3000
|
||||||
DATABASE_CONNECTION_STRING=
|
DATABASE_CONNECTION_STRING=
|
||||||
|
# 生产环境跨子域 Cookie,例如 .uniiem.com
|
||||||
|
COOKIE_DOMAIN=
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ import * as schema from "@/db/schema.ts";
|
|||||||
import { admin, bearer, username } from "better-auth/plugins";
|
import { admin, bearer, username } from "better-auth/plugins";
|
||||||
import { passkey } from "@better-auth/passkey";
|
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({
|
export const auth = betterAuth({
|
||||||
database: drizzleAdapter(useDrizzle(), {
|
database: drizzleAdapter(useDrizzle(), {
|
||||||
provider: "pg",
|
provider: "pg",
|
||||||
@@ -12,7 +15,7 @@ export const auth = betterAuth({
|
|||||||
...schema,
|
...schema,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
trustedOrigins: [process.env.WEB_ORIGIN ?? "http://localhost:3000"],
|
trustedOrigins: [webOrigin],
|
||||||
appName: "Helios EVCS",
|
appName: "Helios EVCS",
|
||||||
user: {
|
user: {
|
||||||
additionalFields: {},
|
additionalFields: {},
|
||||||
@@ -20,8 +23,20 @@ export const auth = betterAuth({
|
|||||||
emailAndPassword: {
|
emailAndPassword: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
},
|
},
|
||||||
plugins: [admin(), username(), bearer(), passkey()],
|
plugins: [
|
||||||
|
admin(),
|
||||||
|
username(),
|
||||||
|
bearer(),
|
||||||
|
passkey({
|
||||||
|
rpID,
|
||||||
|
rpName: "Helios EVCS",
|
||||||
|
origin: webOrigin,
|
||||||
|
}),
|
||||||
|
],
|
||||||
advanced: {
|
advanced: {
|
||||||
cookiePrefix: "helios_auth",
|
cookiePrefix: "helios_auth",
|
||||||
|
crossSubdomainCookies: process.env.COOKIE_DOMAIN
|
||||||
|
? { enabled: true, domain: process.env.COOKIE_DOMAIN }
|
||||||
|
: { enabled: false },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user