@@ -52,7 +52,7 @@ import { EditableTeamMemberProfile, ReceivedTeamInvitation, SentTeamInvitation,
5252import { ActiveSession , Auth , BaseUser , CurrentUser , InternalUserExtra , OAuthProvider , ProjectCurrentUser , SyncedPartialUser , TokenPartialUser , UserExtra , UserUpdateOptions , userUpdateOptionsToCrud , withUserDestructureGuard } from "../../users" ;
5353import { StackClientApp , StackClientAppConstructorOptions , StackClientAppJson } from "../interfaces/client-app" ;
5454import { _StackAdminAppImplIncomplete } from "./admin-app-impl" ;
55- import { TokenObject , clientVersion , createCache , createCacheBySession , createEmptyTokenStore , getAnalyticsBaseUrl , getBaseUrl , getDefaultExtraRequestHeaders , getDefaultProjectId , getDefaultPublishableClientKey , getUrls , resolveConstructorOptions } from "./common" ;
55+ import { LOCAL_EMULATOR_INTERNAL_PUBLISHABLE_CLIENT_KEY , TokenObject , clientVersion , createCache , createCacheBySession , createEmptyTokenStore , fetchEmulatorProjectCredentials , getAnalyticsBaseUrl , getBaseUrl , getDefaultExtraRequestHeaders , getDefaultProjectId , getDefaultPublishableClientKey , getLocalEmulatorConfigFilePath , getUrls , resolveConstructorOptions } from "./common" ;
5656import { EventTracker } from "./event-tracker" ;
5757import { AnalyticsOptions , SessionRecorder , analyticsOptionsFromJson , analyticsOptionsToJson } from "./session-replay" ;
5858
@@ -101,6 +101,7 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
101101 private _sessionRecorder : SessionRecorder | null = null ;
102102 private _eventTracker : EventTracker | null = null ;
103103
104+ protected _emulatorInitPromise : Promise < void > | null = null ;
104105 private __DEMO_ENABLE_SLIGHT_FETCH_DELAY = false ;
105106 private readonly _ownedAdminApps = new DependenciesMap < [ InternalSession , string ] , _StackAdminAppImplIncomplete < false , string > > ( ) ;
106107
@@ -499,29 +500,43 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
499500 this . _options = resolvedOptions ;
500501 this . _extraOptions = extraOptions ;
501502
502- const projectId = resolvedOptions . projectId ?? getDefaultProjectId ( ) ;
503+ const emulatorConfigFilePath = getLocalEmulatorConfigFilePath ( resolvedOptions . localEmulatorConfigFilePath ) ;
504+ const isEmulator = ! ! emulatorConfigFilePath ;
505+
506+ const projectId = resolvedOptions . projectId ?? getDefaultProjectId ( { isEmulator } ) ;
503507 if ( projectId !== "internal" && ! ( projectId . match ( / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 1 - 8 ] [ 0 - 9 a - f ] { 3 } - [ 0 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i) ) ) {
504508 throw new Error ( `Invalid project ID: ${ projectId } . Project IDs must be UUIDs. Please check your environment variables and/or your StackApp.` ) ;
505509 }
506510
507- const publishableClientKey = resolvedOptions . publishableClientKey ?? getDefaultPublishableClientKey ( ) ;
511+ const publishableClientKey = resolvedOptions . publishableClientKey ?? getDefaultPublishableClientKey ( ) ?? ( isEmulator ? LOCAL_EMULATOR_INTERNAL_PUBLISHABLE_CLIENT_KEY : undefined ) ;
508512
509513 if ( extraOptions && extraOptions . interface ) {
510514 this . _interface = extraOptions . interface ;
511515 } else {
512516 this . _interface = new StackClientInterface ( {
513- getBaseUrl : ( ) => getBaseUrl ( resolvedOptions . baseUrl ) ,
514- getAnalyticsBaseUrl : ( ) => getAnalyticsBaseUrl ( getBaseUrl ( resolvedOptions . baseUrl ) ) ,
517+ getBaseUrl : ( ) => getBaseUrl ( resolvedOptions . baseUrl , { isEmulator } ) ,
518+ getAnalyticsBaseUrl : ( ) => getAnalyticsBaseUrl ( getBaseUrl ( resolvedOptions . baseUrl , { isEmulator } ) ) ,
515519 extraRequestHeaders : resolvedOptions . extraRequestHeaders ?? getDefaultExtraRequestHeaders ( ) ,
516520 projectId,
517521 clientVersion,
518522 ...( publishableClientKey != null ? { publishableClientKey } : { } ) ,
519523 prepareRequest : async ( ) => {
524+ if ( this . _emulatorInitPromise ) await this . _emulatorInitPromise ;
520525 await cookies ?.( ) ; // THIS_LINE_PLATFORM next
521526 }
522527 } ) ;
523528 }
524529
530+ if ( isEmulator && ! ( extraOptions && extraOptions . interface ) ) {
531+ const iface = this . _interface ;
532+ this . _emulatorInitPromise = fetchEmulatorProjectCredentials ( emulatorConfigFilePath ! ) . then ( ( data ) => {
533+ iface . _updateEmulatorCredentials ( {
534+ projectId : data . project_id ,
535+ publishableClientKey : data . publishable_client_key ,
536+ } ) ;
537+ } ) ;
538+ }
539+
525540 this . _tokenStoreInit = resolvedOptions . tokenStore ;
526541 this . _redirectMethod = resolvedOptions . redirectMethod || "none" ;
527542 this . _redirectMethod = resolvedOptions . redirectMethod || "nextjs" ; // THIS_LINE_PLATFORM next
0 commit comments