|
1 | 1 | import { betterAuth } from "better-auth"; |
2 | 2 | import { genericOAuth } from "better-auth/plugins"; |
3 | 3 |
|
4 | | -// Read from environment variables to support any OIDC provider |
5 | | -const OIDC_ISSUER = process.env.OIDC_ISSUER_URL || ""; |
6 | | -const OIDC_CLIENT_ID = process.env.OIDC_CLIENT_ID || ""; |
7 | | -const OIDC_CLIENT_SECRET = process.env.OIDC_CLIENT_SECRET || ""; |
8 | | -const BETTER_AUTH_SECRET = |
9 | | - process.env.BETTER_AUTH_SECRET || "build-time-placeholder"; |
10 | | -const BETTER_AUTH_URL = process.env.BETTER_AUTH_URL || "http://localhost:3000"; |
| 4 | +const OIDC_PROVIDER_ID = process.env.OIDC_PROVIDER_ID || "oidc"; |
| 5 | +const OIDC_ISSUER = process.env.OIDC_ISSUER || ""; |
| 6 | +const BASE_URL = process.env.BETTER_AUTH_URL || "http://localhost:3000"; |
11 | 7 |
|
12 | | -// Validate required environment variables (warnings only during build) |
13 | | -const isBuild = process.env.NEXT_PHASE === "phase-production-build"; |
14 | | - |
15 | | -if (!process.env.BETTER_AUTH_SECRET) { |
16 | | - const message = |
17 | | - "[Better Auth] BETTER_AUTH_SECRET is required. Set it in .env.local to a strong, random value."; |
18 | | - if (isBuild) { |
19 | | - console.warn(message); |
20 | | - } else { |
21 | | - throw new Error(message); |
22 | | - } |
23 | | -} |
24 | | - |
25 | | -if (!OIDC_ISSUER || !OIDC_CLIENT_ID || !OIDC_CLIENT_SECRET) { |
26 | | - const message = |
27 | | - "[Better Auth] OIDC configuration is incomplete. Set OIDC_ISSUER_URL, OIDC_CLIENT_ID, and OIDC_CLIENT_SECRET in .env.local"; |
28 | | - if (isBuild) { |
29 | | - console.warn(message); |
30 | | - } else { |
31 | | - throw new Error(message); |
32 | | - } |
33 | | -} |
34 | | - |
35 | | -console.log("[Better Auth] OIDC Configuration:", { |
36 | | - issuer: OIDC_ISSUER, |
37 | | - clientId: OIDC_CLIENT_ID, |
38 | | - baseURL: BETTER_AUTH_URL, |
39 | | - discoveryUrl: `${OIDC_ISSUER}/.well-known/openid-configuration`, |
40 | | - callbackURL: `${BETTER_AUTH_URL}/api/auth/oauth2/callback/oidc`, |
41 | | -}); |
42 | | - |
43 | | -// Configure trusted origins - defaults to localhost ports for development |
44 | | -// Set TRUSTED_ORIGINS environment variable for production (comma-separated list) |
45 | 8 | const trustedOrigins = process.env.TRUSTED_ORIGINS |
46 | | - ? process.env.TRUSTED_ORIGINS.split(",").map((origin) => origin.trim()) |
47 | | - : [ |
48 | | - "http://localhost:3000", |
49 | | - "http://localhost:3001", |
50 | | - "http://localhost:3002", |
51 | | - "http://localhost:3003", |
52 | | - ]; |
| 9 | + ? process.env.TRUSTED_ORIGINS.split(",").map((s) => s.trim()) |
| 10 | + : [BASE_URL, "http://localhost:3002", "http://localhost:3003"]; |
53 | 11 |
|
54 | | -// Always include BETTER_AUTH_URL if not already present |
55 | | -if (BETTER_AUTH_URL && !trustedOrigins.includes(BETTER_AUTH_URL)) { |
56 | | - trustedOrigins.push(BETTER_AUTH_URL); |
| 12 | +if (!trustedOrigins.includes(BASE_URL)) { |
| 13 | + trustedOrigins.push(BASE_URL); |
57 | 14 | } |
58 | 15 |
|
59 | 16 | export const auth = betterAuth({ |
60 | | - secret: BETTER_AUTH_SECRET, |
61 | | - baseURL: BETTER_AUTH_URL, |
| 17 | + secret: process.env.BETTER_AUTH_SECRET || "build-time-placeholder", |
| 18 | + baseURL: BASE_URL, |
62 | 19 | trustedOrigins, |
63 | | - // No database configuration - running in stateless mode |
64 | 20 | session: { |
65 | | - expiresIn: 60 * 60 * 24 * 7, // 7 days |
| 21 | + expiresIn: 60 * 60 * 24 * 7, |
66 | 22 | cookieCache: { |
67 | 23 | enabled: true, |
68 | | - maxAge: 30 * 24 * 60 * 60, // 30 days cache duration |
69 | | - strategy: "jwe", // Use encrypted tokens for better security |
70 | | - refreshCache: true, // Enable stateless refresh |
| 24 | + maxAge: 5 * 60, |
71 | 25 | }, |
72 | 26 | }, |
73 | 27 | plugins: [ |
74 | 28 | genericOAuth({ |
75 | 29 | config: [ |
76 | 30 | { |
77 | | - providerId: "oidc", |
| 31 | + providerId: OIDC_PROVIDER_ID, |
78 | 32 | discoveryUrl: `${OIDC_ISSUER}/.well-known/openid-configuration`, |
79 | | - clientId: OIDC_CLIENT_ID, |
80 | | - clientSecret: OIDC_CLIENT_SECRET, |
| 33 | + authorizationUrl: `${OIDC_ISSUER}/v1/authorize`, |
| 34 | + tokenUrl: `${OIDC_ISSUER}/v1/token`, |
| 35 | + userInfoUrl: `${OIDC_ISSUER}/v1/userinfo`, |
| 36 | + redirectURI: `${BASE_URL}/api/auth/oauth2/callback/${OIDC_PROVIDER_ID}`, |
| 37 | + clientId: process.env.OIDC_CLIENT_ID || "", |
| 38 | + clientSecret: process.env.OIDC_CLIENT_SECRET || "", |
81 | 39 | scopes: ["openid", "email", "profile"], |
| 40 | + pkce: false, |
82 | 41 | }, |
83 | 42 | ], |
84 | 43 | }), |
|
0 commit comments