Skip to content

Commit

Permalink
fix(@dpc-sdp/nuxt-ripple-preview): only send token if it's not expired
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdowdle authored and dylankelly committed Oct 30, 2023
1 parent fb22651 commit 90528b8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/nuxt-ripple/composables/use-tide-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export const useTidePage = async (
// Need to manually pass the cookies needed for auth as they aren't automatically added when server rendered
if (isPreviewPath(path)) {
const accessTokenCookie = useCookie(AuthCookieNames.ACCESS_TOKEN)
headers.cookie = `${AuthCookieNames.ACCESS_TOKEN}=${accessTokenCookie.value};`
const accessTokenExpiryCookie = useCookie(
AuthCookieNames.ACCESS_TOKEN_EXPIRY
)
headers.cookie = `${AuthCookieNames.ACCESS_TOKEN}=${accessTokenCookie.value};${AuthCookieNames.ACCESS_TOKEN_EXPIRY}=${accessTokenExpiryCookie.value}`
}

let sectionCacheTags
Expand Down
10 changes: 9 additions & 1 deletion packages/nuxt-ripple/server/api/tide/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ export const createPageHandler = async (
}

const tokenCookie = getCookie(event, AuthCookieNames.ACCESS_TOKEN)
const accessTokenExpiry = parseFloat(
getCookie(event, AuthCookieNames.ACCESS_TOKEN_EXPIRY)
)
const isTokenExpired = accessTokenExpiry
? accessTokenExpiry < Date.now()
: true

const headers = {}

if (tokenCookie) {
// Only pass the access token if it is not expired, otherwise it will be rejected by the API
if (tokenCookie && !isTokenExpired) {
headers['X-OAuth2-Authorization'] = `Bearer ${tokenCookie}`
}

Expand Down

0 comments on commit 90528b8

Please sign in to comment.