|
| 1 | +import type { BetterAuthOptions } from "better-auth"; |
1 | 2 | import { betterAuth } from "better-auth"; |
2 | 3 | import { genericOAuth } from "better-auth/plugins"; |
3 | 4 |
|
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"; |
| 5 | +const OIDC_PROVIDER_ID = process.env.OIDC_PROVIDER_ID || "oidc"; |
| 6 | +const OIDC_ISSUER = process.env.OIDC_ISSUER || ""; |
| 7 | +const BASE_URL = process.env.BETTER_AUTH_URL || "http://localhost:3000"; |
11 | 8 |
|
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 | 9 | 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 | | - ]; |
| 10 | + ? process.env.TRUSTED_ORIGINS.split(",").map((s) => s.trim()) |
| 11 | + : [BASE_URL, "http://localhost:3002", "http://localhost:3003"]; |
53 | 12 |
|
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); |
| 13 | +if (!trustedOrigins.includes(BASE_URL)) { |
| 14 | + trustedOrigins.push(BASE_URL); |
57 | 15 | } |
58 | 16 |
|
59 | 17 | export const auth = betterAuth({ |
60 | | - secret: BETTER_AUTH_SECRET, |
61 | | - baseURL: BETTER_AUTH_URL, |
| 18 | + secret: process.env.BETTER_AUTH_SECRET || "build-time-placeholder", |
| 19 | + baseURL: BASE_URL, |
62 | 20 | trustedOrigins, |
63 | | - // No database configuration - running in stateless mode |
64 | 21 | session: { |
65 | | - expiresIn: 60 * 60 * 24 * 7, // 7 days |
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 |
71 | 24 | }, |
72 | 25 | }, |
73 | 26 | plugins: [ |
74 | 27 | genericOAuth({ |
75 | 28 | config: [ |
76 | 29 | { |
77 | | - providerId: "oidc", |
| 30 | + providerId: OIDC_PROVIDER_ID, |
78 | 31 | discoveryUrl: `${OIDC_ISSUER}/.well-known/openid-configuration`, |
79 | | - clientId: OIDC_CLIENT_ID, |
80 | | - clientSecret: OIDC_CLIENT_SECRET, |
| 32 | + redirectURI: `${BASE_URL}/api/auth/oauth2/callback/${OIDC_PROVIDER_ID}`, |
| 33 | + clientId: process.env.OIDC_CLIENT_ID || "", |
| 34 | + clientSecret: process.env.OIDC_CLIENT_SECRET || "", |
81 | 35 | scopes: ["openid", "email", "profile"], |
| 36 | + pkce: true, |
82 | 37 | }, |
83 | 38 | ], |
84 | 39 | }), |
85 | 40 | ], |
86 | | -}); |
| 41 | +} as BetterAuthOptions); |
0 commit comments