@@ -3,6 +3,7 @@ import type { AstroIntegration } from 'astro';
33import { envField } from 'astro/config' ;
44
55import { name as packageName , version as packageVersion } from '../../package.json' ;
6+ import { resolveKeysWithKeylessFallback } from '../server/keyless/utils' ;
67import type { AstroClerkIntegrationParams } from '../types' ;
78import { vitePluginAstroConfig } from './vite-plugin-astro-config' ;
89
@@ -26,17 +27,36 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
2627 return {
2728 name : '@clerk/astro/integration' ,
2829 hooks : {
29- 'astro:config:setup' : ( { config, injectScript, updateConfig, logger, command } ) => {
30+ 'astro:config:setup' : async ( { config, injectScript, updateConfig, logger, command } ) => {
3031 if ( [ 'server' , 'hybrid' ] . includes ( config . output ) && ! config . adapter ) {
3132 logger . error ( 'Missing adapter, please update your Astro config to use one.' ) ;
3233 }
3334
35+ const envPublishableKey = process . env . PUBLIC_CLERK_PUBLISHABLE_KEY ;
36+ const envSecretKey = process . env . CLERK_SECRET_KEY ;
37+
38+ const isDev = command === 'dev' ;
39+ let resolvedKeys = {
40+ publishableKey : envPublishableKey ,
41+ secretKey : envSecretKey ,
42+ claimUrl : undefined as string | undefined ,
43+ apiKeysUrl : undefined as string | undefined ,
44+ } ;
45+
46+ if ( isDev ) {
47+ try {
48+ resolvedKeys = await resolveKeysWithKeylessFallback ( envPublishableKey , envSecretKey ) ;
49+ } catch {
50+ logger . warn ( 'Keyless mode initialization failed, using configured keys' ) ;
51+ }
52+ }
53+
3454 const internalParams : ClerkOptions = {
3555 ...params ,
3656 sdkMetadata : {
3757 version : packageVersion ,
3858 name : packageName ,
39- environment : command === 'dev' ? 'development' : 'production' ,
59+ environment : isDev ? 'development' : 'production' ,
4060 } ,
4161 } ;
4262
@@ -58,6 +78,10 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
5878 ...buildEnvVarFromOption ( clerkJSUrl , 'PUBLIC_CLERK_JS_URL' ) ,
5979 ...buildEnvVarFromOption ( clerkJSVersion , 'PUBLIC_CLERK_JS_VERSION' ) ,
6080 ...buildEnvVarFromOption ( prefetchUI === false ? 'false' : undefined , 'PUBLIC_CLERK_PREFETCH_UI' ) ,
81+ ...buildEnvVarFromOption ( resolvedKeys . publishableKey , 'PUBLIC_CLERK_PUBLISHABLE_KEY' ) ,
82+ ...buildEnvVarFromOption ( resolvedKeys . secretKey , 'CLERK_SECRET_KEY' ) ,
83+ ...buildEnvVarFromOption ( resolvedKeys . claimUrl , 'PUBLIC_CLERK_KEYLESS_CLAIM_URL' ) ,
84+ ...buildEnvVarFromOption ( resolvedKeys . apiKeysUrl , 'PUBLIC_CLERK_KEYLESS_API_KEYS_URL' ) ,
6185 } ,
6286
6387 ssr : {
@@ -157,7 +181,7 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
157181
158182function createClerkEnvSchema ( ) {
159183 return {
160- PUBLIC_CLERK_PUBLISHABLE_KEY : envField . string ( { context : 'client' , access : 'public' } ) ,
184+ PUBLIC_CLERK_PUBLISHABLE_KEY : envField . string ( { context : 'client' , access : 'public' , optional : true } ) ,
161185 PUBLIC_CLERK_SIGN_IN_URL : envField . string ( { context : 'client' , access : 'public' , optional : true } ) ,
162186 PUBLIC_CLERK_SIGN_UP_URL : envField . string ( { context : 'client' , access : 'public' , optional : true } ) ,
163187 PUBLIC_CLERK_IS_SATELLITE : envField . boolean ( { context : 'client' , access : 'public' , optional : true } ) ,
@@ -169,7 +193,20 @@ function createClerkEnvSchema() {
169193 PUBLIC_CLERK_UI_URL : envField . string ( { context : 'client' , access : 'public' , optional : true , url : true } ) ,
170194 PUBLIC_CLERK_TELEMETRY_DISABLED : envField . boolean ( { context : 'client' , access : 'public' , optional : true } ) ,
171195 PUBLIC_CLERK_TELEMETRY_DEBUG : envField . boolean ( { context : 'client' , access : 'public' , optional : true } ) ,
172- CLERK_SECRET_KEY : envField . string ( { context : 'server' , access : 'secret' } ) ,
196+ PUBLIC_CLERK_KEYLESS_CLAIM_URL : envField . string ( {
197+ context : 'client' ,
198+ access : 'public' ,
199+ optional : true ,
200+ url : true ,
201+ } ) ,
202+ PUBLIC_CLERK_KEYLESS_API_KEYS_URL : envField . string ( {
203+ context : 'client' ,
204+ access : 'public' ,
205+ optional : true ,
206+ url : true ,
207+ } ) ,
208+ PUBLIC_CLERK_KEYLESS_DISABLED : envField . boolean ( { context : 'client' , access : 'public' , optional : true } ) ,
209+ CLERK_SECRET_KEY : envField . string ( { context : 'server' , access : 'secret' , optional : true } ) ,
173210 CLERK_MACHINE_SECRET_KEY : envField . string ( { context : 'server' , access : 'secret' , optional : true } ) ,
174211 CLERK_JWT_KEY : envField . string ( { context : 'server' , access : 'secret' , optional : true } ) ,
175212 } ;
0 commit comments