@@ -102,29 +102,38 @@ function createClient(
102102async function syncAuthState ( ) : Promise < void > {
103103 const authState = await trpcClient . auth . getState . query ( ) ;
104104 const isAuthenticated = authState . status === "authenticated" ;
105- const client =
106- isAuthenticated && authState . cloudRegion
107- ? createClient ( authState . cloudRegion , authState . projectId )
108- : null ;
109-
110- useAuthStore . setState ( ( state ) => ( {
111- ...state ,
112- isAuthenticated,
113- cloudRegion : authState . cloudRegion ,
114- staleCloudRegion : isAuthenticated
115- ? null
116- : ( authState . cloudRegion ?? state . staleCloudRegion ) ,
117- client,
118- projectId : authState . projectId ,
119- availableProjectIds : authState . availableProjectIds ,
120- availableOrgIds : authState . availableOrgIds ,
121- needsProjectSelection :
122- isAuthenticated &&
123- authState . availableProjectIds . length > 1 &&
124- authState . projectId === null ,
125- needsScopeReauth : authState . needsScopeReauth ,
126- hasCodeAccess : authState . hasCodeAccess ,
127- } ) ) ;
105+
106+ useAuthStore . setState ( ( state ) => {
107+ const regionChanged = authState . cloudRegion !== state . cloudRegion ;
108+ const projectChanged = authState . projectId !== state . projectId ;
109+ const client =
110+ isAuthenticated && authState . cloudRegion
111+ ? regionChanged || projectChanged || ! state . client
112+ ? createClient ( authState . cloudRegion , authState . projectId )
113+ : state . client
114+ : null ;
115+
116+ return {
117+ ...state ,
118+ isAuthenticated,
119+ cloudRegion : authState . cloudRegion ,
120+ staleCloudRegion : isAuthenticated
121+ ? null
122+ : ( authState . cloudRegion ?? state . staleCloudRegion ) ,
123+ client,
124+ projectId : authState . projectId ,
125+ availableProjectIds : authState . availableProjectIds ,
126+ availableOrgIds : authState . availableOrgIds ,
127+ needsProjectSelection :
128+ isAuthenticated &&
129+ authState . availableProjectIds . length > 1 &&
130+ authState . projectId === null ,
131+ needsScopeReauth : authState . needsScopeReauth ,
132+ hasCodeAccess : authState . hasCodeAccess ,
133+ } ;
134+ } ) ;
135+
136+ const client = useAuthStore . getState ( ) . client ;
128137
129138 if ( ! isAuthenticated || ! authState . cloudRegion || ! client ) {
130139 inFlightAuthSync = null ;
0 commit comments