@@ -2,15 +2,22 @@ import { betterAuth } from "better-auth";
22import { genericOAuth } from "better-auth/plugins" ;
33
44// 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 = process . env . BETTER_AUTH_SECRET || "ChangeMePlease" ;
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 = process . env . BETTER_AUTH_SECRET ;
99const BETTER_AUTH_URL = process . env . BETTER_AUTH_URL || "http://localhost:3000" ;
1010
11+ // Validate required environment variables
12+ if ( ! BETTER_AUTH_SECRET ) {
13+ throw new Error (
14+ "[Better Auth] BETTER_AUTH_SECRET is required. Set it in .env.local to a strong, random value." ,
15+ ) ;
16+ }
17+
1118if ( ! OIDC_ISSUER || ! OIDC_CLIENT_ID || ! OIDC_CLIENT_SECRET ) {
12- console . warn (
13- "[Better Auth] Missing OIDC configuration. Set OIDC_ISSUER_URL, OIDC_CLIENT_ID, and OIDC_CLIENT_SECRET in .env.local" ,
19+ throw new Error (
20+ "[Better Auth] OIDC configuration is incomplete . Set OIDC_ISSUER_URL, OIDC_CLIENT_ID, and OIDC_CLIENT_SECRET in .env.local" ,
1421 ) ;
1522}
1623
@@ -22,15 +29,26 @@ console.log("[Better Auth] OIDC Configuration:", {
2229 callbackURL : `${ BETTER_AUTH_URL } /api/auth/oauth2/callback/oidc` ,
2330} ) ;
2431
32+ // Configure trusted origins - defaults to localhost ports for development
33+ // Set TRUSTED_ORIGINS environment variable for production (comma-separated list)
34+ const trustedOrigins = process . env . TRUSTED_ORIGINS
35+ ? process . env . TRUSTED_ORIGINS . split ( "," ) . map ( ( origin ) => origin . trim ( ) )
36+ : [
37+ "http://localhost:3000" ,
38+ "http://localhost:3001" ,
39+ "http://localhost:3002" ,
40+ "http://localhost:3003" ,
41+ ] ;
42+
43+ // Always include BETTER_AUTH_URL if not already present
44+ if ( BETTER_AUTH_URL && ! trustedOrigins . includes ( BETTER_AUTH_URL ) ) {
45+ trustedOrigins . push ( BETTER_AUTH_URL ) ;
46+ }
47+
2548export const auth = betterAuth ( {
2649 secret : BETTER_AUTH_SECRET ,
2750 baseURL : BETTER_AUTH_URL ,
28- trustedOrigins : [
29- "http://localhost:3000" ,
30- "http://localhost:3001" ,
31- "http://localhost:3002" ,
32- "http://localhost:3003" ,
33- ] ,
51+ trustedOrigins,
3452 // No database configuration - running in stateless mode
3553 session : {
3654 expiresIn : 60 * 60 * 24 * 7 , // 7 days
0 commit comments