|
1 | 1 | import type { ClerkOptions } from '@clerk/shared/types'; |
2 | 2 | import type { AstroIntegration } from 'astro'; |
3 | 3 | import { envField } from 'astro/config'; |
| 4 | +import { loadEnv } from 'vite'; |
4 | 5 |
|
5 | 6 | import { name as packageName, version as packageVersion } from '../../package.json'; |
6 | 7 | import { resolveKeysWithKeylessFallback } from '../server/keyless/utils'; |
@@ -32,10 +33,24 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>() |
32 | 33 | logger.error('Missing adapter, please update your Astro config to use one.'); |
33 | 34 | } |
34 | 35 |
|
35 | | - const envPublishableKey = process.env.PUBLIC_CLERK_PUBLISHABLE_KEY; |
36 | | - const envSecretKey = process.env.CLERK_SECRET_KEY; |
37 | | - |
38 | 36 | const isDev = command === 'dev'; |
| 37 | + |
| 38 | + // Load environment variables from .env files |
| 39 | + // Astro's integration hook runs before .env is loaded, so we need to do it manually |
| 40 | + const mode = isDev ? 'development' : 'production'; |
| 41 | + const envDir = config.root.pathname; |
| 42 | + const loadedEnv = loadEnv(mode, envDir, ''); |
| 43 | + |
| 44 | + console.log('[Integration Debug] Loaded env from .env files:', { |
| 45 | + PUBLIC_CLERK_PUBLISHABLE_KEY: loadedEnv.PUBLIC_CLERK_PUBLISHABLE_KEY?.substring(0, 20) + '...', |
| 46 | + CLERK_SECRET_KEY: loadedEnv.CLERK_SECRET_KEY?.substring(0, 20) + '...', |
| 47 | + allClerkKeys: Object.keys(loadedEnv).filter(k => k.includes('CLERK')), |
| 48 | + }); |
| 49 | + |
| 50 | + // Try .env files first, then fall back to process.env |
| 51 | + const envPublishableKey = loadedEnv.PUBLIC_CLERK_PUBLISHABLE_KEY || process.env.PUBLIC_CLERK_PUBLISHABLE_KEY; |
| 52 | + const envSecretKey = loadedEnv.CLERK_SECRET_KEY || process.env.CLERK_SECRET_KEY; |
| 53 | + |
39 | 54 | let resolvedKeys = { |
40 | 55 | publishableKey: envPublishableKey, |
41 | 56 | secretKey: envSecretKey, |
|
0 commit comments