diff --git a/packages/nuxt-ripple/server/api/tide/page.ts b/packages/nuxt-ripple/server/api/tide/page.ts index 9b96b784d1..746d454690 100644 --- a/packages/nuxt-ripple/server/api/tide/page.ts +++ b/packages/nuxt-ripple/server/api/tide/page.ts @@ -9,10 +9,7 @@ import { import { createHandler, TidePageApi } from '@dpc-sdp/ripple-tide-api' import { BadRequestError } from '@dpc-sdp/ripple-tide-api/errors' import { useNitroApp } from '#imports' -import { - isPreviewPath, - AuthCookieNames -} from '@dpc-sdp/nuxt-ripple-preview/utils' +import { AuthCookieNames } from '@dpc-sdp/nuxt-ripple-preview/utils' export const createPageHandler = async ( event: H3Event, @@ -32,8 +29,7 @@ export const createPageHandler = async ( const tokenCookie = getCookie(event, AuthCookieNames.ACCESS_TOKEN) const headers = {} - // Only add the access token to the headers if the path is a preview path - if (isPreviewPath(query.path)) { + if (tokenCookie) { headers['X-OAuth2-Authorization'] = `Bearer ${tokenCookie}` } @@ -45,7 +41,7 @@ export const createPageHandler = async ( ) // Need to pass on the section cache tags to the nuxt app - if (pageResponse.headers) { + if (pageResponse.headers && pageResponse.headers['section-cache-tags']) { setResponseHeader( event, 'section-cache-tags', diff --git a/packages/nuxt-ripple/server/api/tide/site.ts b/packages/nuxt-ripple/server/api/tide/site.ts index 4140319f99..23013fd6f3 100644 --- a/packages/nuxt-ripple/server/api/tide/site.ts +++ b/packages/nuxt-ripple/server/api/tide/site.ts @@ -18,11 +18,13 @@ export const createSiteHandler = async ( const siteResponse = await tideSiteApi.getSiteData(query.id) // Need to pass on the section cache tags to the nuxt app - setResponseHeader( - event, - 'section-cache-tags', - siteResponse.headers['section-cache-tags'] - ) + if (siteResponse.headers && siteResponse.headers['section-cache-tags']) { + setResponseHeader( + event, + 'section-cache-tags', + siteResponse.headers['section-cache-tags'] + ) + } return siteResponse.data }) diff --git a/packages/ripple-tide-api/src/services/tide-page.ts b/packages/ripple-tide-api/src/services/tide-page.ts index 2bd4b160ba..d750159cf0 100644 --- a/packages/ripple-tide-api/src/services/tide-page.ts +++ b/packages/ripple-tide-api/src/services/tide-page.ts @@ -84,12 +84,12 @@ export default class TidePageApi extends TideApiBase { } } - async getRouteByPath(path: string, site: string = this.site) { + async getRouteByPath(path: string, site: string = this.site, headers = {}) { this.path = path const routeUrl = `/route?site=${site}&path=${path}` - return this.get(routeUrl) + return this.get(routeUrl, { headers }) .then((response) => response?.data?.data?.attributes) .catch((error) => { throw new NotFoundError( @@ -117,7 +117,7 @@ export default class TidePageApi extends TideApiBase { return this.getPageFromPreviewLink(path, site, params, headers) } - const route = await this.getRouteByPath(path, site) + const route = await this.getRouteByPath(path, site, headers) if (route && !route.error) { if (route.hasOwnProperty('redirect_type')) { return { @@ -135,7 +135,7 @@ export default class TidePageApi extends TideApiBase { if (includes !== '') { fullParams['include'] = includes } - return this.getPageByRouteData(route, { params: fullParams }) + return this.getPageByRouteData(route, { params: fullParams, headers }) } } diff --git a/packages/ripple-tide-publication/server/api/tide/publication-index.ts b/packages/ripple-tide-publication/server/api/tide/publication-index.ts index 33683fe71a..2e59d3e61d 100644 --- a/packages/ripple-tide-publication/server/api/tide/publication-index.ts +++ b/packages/ripple-tide-publication/server/api/tide/publication-index.ts @@ -9,6 +9,7 @@ import type { } from '@dpc-sdp/ripple-tide-api/types' import type { indexNode, apiNode } from '../../../types' import { useRuntimeConfig } from '#imports' +import { AuthCookieNames } from '@dpc-sdp/nuxt-ripple-preview/utils' /** * @description Recursively transform API response to match component props @@ -51,12 +52,13 @@ class TidePublicationIndexApi extends TideApiBase { this.logLabel = 'TidePublicationIndex' } - async getPublicationMenu(id: string) { + async getPublicationMenu(id: string, headers = {}) { try { const { data: response } = await this.get( `/node/publication/${id}/hierarchy`, { - params: { site: this.siteId } + params: { site: this.siteId }, + headers } ) const resource = jsonapiParse.parse(response).data.meta.hierarchy @@ -83,7 +85,14 @@ export const createPublicationIndexHandler = async ( throw new BadRequestError('Publication ID is required') } - return await publicationIndexApi.getPublicationMenu(query.id) + const tokenCookie = getCookie(event, AuthCookieNames.ACCESS_TOKEN) + const headers = {} + + if (tokenCookie) { + headers['X-OAuth2-Authorization'] = `Bearer ${tokenCookie}` + } + + return await publicationIndexApi.getPublicationMenu(query.id, headers) }) }