@@ -95,6 +95,26 @@ export class ExperienceSite {
9595 return experienceSites ;
9696 }
9797
98+ /**
99+ * Steps to init:
100+ * 1) Verify the site is an actual site within this organization
101+ * 2) Verify and output specific message if wrong site type is selected
102+ * 2) Verify that site has been published with Local Dev perm enabled (i.e. a static resource exists for this site name)
103+ * 3) Establish sid token for proxied requests
104+ */
105+ // public async init(): Promise<void> {}
106+
107+ /**
108+ * Esablish a valid token for this local development session
109+ *
110+ * @returns sid token for proxied site requests
111+ */
112+ public async setupAuth ( ) : Promise < string > {
113+ const networkId = await this . getNetworkId ( ) ;
114+ const sidToken = await this . getNewSidToken ( networkId ) ;
115+ return sidToken ;
116+ }
117+
98118 public async isUpdateAvailable ( ) : Promise < boolean > {
99119 const localMetadata = this . getLocalMetadata ( ) ;
100120 if ( ! localMetadata ) {
@@ -227,10 +247,8 @@ export class ExperienceSite {
227247 return resourcePath ;
228248 }
229249
230- // TODO cleanup
231- public async getNetworkIdByName ( ) : Promise < string > {
250+ public async getNetworkId ( ) : Promise < string > {
232251 const conn = this . org . getConnection ( ) ;
233- // try {
234252 // Query the Network object for the network with the given site name
235253 const result = await conn . query < { Id : string } > ( `SELECT Id FROM Network WHERE Name = '${ this . siteDisplayName } '` ) ;
236254
@@ -243,48 +261,30 @@ export class ExperienceSite {
243261 } else {
244262 throw new Error ( `Network with name '${ this . siteDisplayName } ' not found` ) ;
245263 }
246- // } catch (error) {
247- // // console.error('Error fetching Network ID:', error);
248- // throw error;
249- // }
250264 }
251265
252266 public async getNewSidToken ( networkId : string ) : Promise < string > {
253267 // Get the connection and access token from the org
254268 const conn = this . org . getConnection ( ) ;
255- const identity = await conn . identity ( ) ;
256- if ( identity . user_id ) {
257- // do something
258- }
259269 const orgId = this . org . getOrgId ( ) ;
270+
271+ // Not sure if we need to do this
260272 const orgIdMinus3 = orgId . substring ( 0 , orgId . length - 3 ) ;
261- const accessToken = conn . accessToken ;
262- const instanceUrl = conn . instanceUrl ;
273+ const accessToken = conn . accessToken ; // TODO should we be refreshing before use?
274+ const instanceUrl = conn . instanceUrl ; // Org URL
263275
264- // Construct the switch URL
276+ // Call out to the servlet to establish a session
265277 const switchUrl = `${ instanceUrl } /servlet/networks/switch?networkId=${ networkId } ` ;
266278
267- // try {
268279 // Make the GET request without following redirects
269280 if ( accessToken ) {
270- const cookies = [
271- `sid=${ accessToken } ` ,
272- `oid=${ orgIdMinus3 } ` ,
273- // 'sid_Client=s000000uuCPw000000I7xV',
274- // Include other essential cookies if necessary
275- // For example:
276- // `oid=${conn.getAuthInfoFields().orgId}`,
277- // `sid_Client=${conn.userInfo.id}`,
278- // Add any other cookies that might be required
279- ]
280- . join ( '; ' )
281- . trim ( ) ;
281+ // TODO should we always refreshAuth?
282+ // await conn.refreshAuth();
283+
284+ const cookies = [ `sid=${ accessToken } ` , `oid=${ orgIdMinus3 } ` ] . join ( '; ' ) . trim ( ) ;
282285 let response = await axios . get ( switchUrl , {
283286 headers : {
284287 Cookie : cookies ,
285- // Include other headers if necessary
286- // 'User-Agent': 'Your User Agent String',
287- // 'Referer': 'Referer URL if required',
288288 } ,
289289 withCredentials : true ,
290290 maxRedirects : 0 , // Prevent axios from following redirects
@@ -294,32 +294,15 @@ export class ExperienceSite {
294294 // Extract the Location header
295295 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
296296 const locationHeader = response . headers [ 'location' ] ;
297-
298297 if ( locationHeader ) {
299298 // Parse the URL to extract the 'sid' parameter
300299 // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
301300 const urlObj = new URL ( locationHeader ) ;
302301 const sid = urlObj . searchParams . get ( 'sid' ) ?? '' ;
303- const cookies2 = [
304- '__Secure-has-sid=1' ,
305- `sid=${ sid } ` ,
306- `oid=${ orgIdMinus3 } ` ,
307- // 'sid_Client=s000000uuCPw000000I7xV',
308- // Include other essential cookies if necessary
309- // For example:
310- // `oid=${conn.getAuthInfoFields().orgId}`,
311- // `sid_Client=${conn.userInfo.id}`,
312- // Add any other cookies that might be required
313- ]
314- . join ( '; ' )
315- . trim ( ) ;
316-
302+ const cookies2 = [ '__Secure-has-sid=1' , `sid=${ sid } ` , `oid=${ orgIdMinus3 } ` ] . join ( '; ' ) . trim ( ) ;
317303 response = await axios . get ( urlObj . toString ( ) , {
318304 headers : {
319305 Cookie : cookies2 ,
320- // Include other headers if necessary
321- // 'User-Agent': 'Your User Agent String',
322- // 'Referer': 'Referer URL if required',
323306 } ,
324307 withCredentials : true ,
325308 maxRedirects : 0 , // Prevent axios from following redirects
@@ -330,7 +313,6 @@ export class ExperienceSite {
330313 // 'set-cookie' can be an array if multiple cookies are set
331314 // Find the 'sid' cookie in the set-cookie header
332315 const sidCookie = setCookieHeader . find ( ( cookieStr : string ) => cookieStr . startsWith ( 'sid=' ) ) ;
333-
334316 if ( sidCookie ) {
335317 // Extract the sid value from the cookie string
336318 const sidMatch = sidCookie . match ( / s i d = ( [ ^ ; ] + ) / ) ;
@@ -339,26 +321,21 @@ export class ExperienceSite {
339321 return sidToken ;
340322 }
341323 }
342- } else {
343- // eslint-disable-next-line no-console
344- console . log ( 'error couldnt find set-cookie header for sid token' ) ;
345- }
346-
347- if ( sid ) {
348- return sid ;
349- } else {
350- throw new Error ( 'SID token not found in Location header' ) ;
351324 }
352- } else {
353- throw new Error ( 'Location header not found in response' ) ;
354325 }
326+ // eslint-disable-next-line no-console
327+ console . warn (
328+ `Warning: could not establish valid auth token for your site '${ this . siteDisplayName } '.` +
329+ 'Local Dev proxied requests to your site may fail or return data from the guest user context.'
330+ ) ;
331+
332+ return accessToken ;
355333 }
334+ // eslint-disable-next-line no-console
335+ console . warn (
336+ 'Warning: sf cli org connection missing accessToken. Local Dev proxied requests to your site may fail or return data from the guest user context.'
337+ ) ;
356338 return '' ;
357- // } catch (error) {
358- // // Handle errors (e.g., network issues, HTTP errors)
359- // // console.error('Error obtaining new SID token:', error);
360- // throw error;
361- // }
362339 }
363340}
364341
0 commit comments