Skip to content

Commit

Permalink
Merge pull request #4778 from Shopify/jm/add-storefront-session-logs
Browse files Browse the repository at this point in the history
Add debug logs for storefront session errors
  • Loading branch information
jamesmengo authored Nov 1, 2024
2 parents 0f8840e + dc14fd9 commit 23daedb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ describe('Storefront API', () => {
response({
status: 200,
headers: {'set-cookie': ''},
text: () => Promise.resolve(''),
}),
)
.mockResolvedValueOnce(
response({
status: 200,
headers: {'set-cookie': 'storefront_digest=digest-value; path=/; HttpOnly'},
text: () => Promise.resolve(''),
}),
)

Expand All @@ -105,9 +107,15 @@ describe('Storefront API', () => {
response({
status: 200,
headers: {'set-cookie': '_shopify_essential=:AABBCCDDEEFFGGHH==123:; path=/; HttpOnly'},
text: () => Promise.resolve(''),
}),
)
.mockResolvedValueOnce(
response({
status: 401,
text: () => Promise.resolve(''),
}),
)
.mockResolvedValueOnce(response({status: 401}))

// When
const cookies = getStorefrontSessionCookies('https://example-store.myshopify.com', '123456', 'wrongpassword')
Expand All @@ -121,7 +129,7 @@ describe('Storefront API', () => {

// Tests rely on this function because the 'packages/theme' package cannot
// directly access node-fetch and they use: new Response('OK', {status: 200})
function response(mock: {status: number; headers?: {[key: string]: string}}) {
function response(mock: {status: number; headers?: {[key: string]: string}; text?: () => Promise<string>}) {
const setCookieHeader = (mock.headers ?? {})['set-cookie'] ?? ''
const setCookieArray = [setCookieHeader]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {parseCookies, serializeCookies} from './cookies.js'
import {defaultHeaders} from './storefront-utils.js'
import {fetch} from '@shopify/cli-kit/node/http'
import {AbortError} from '@shopify/cli-kit/node/error'
import {outputDebug} from '@shopify/cli-kit/node/output'

export async function isStorefrontPasswordProtected(storeURL: string): Promise<boolean> {
const response = await fetch(prependHttps(storeURL), {
Expand Down Expand Up @@ -104,6 +105,14 @@ async function sessionEssentialCookie(storeUrl: string, themeId: string, headers
const setCookies = response.headers.raw()['set-cookie'] ?? []
const shopifyEssential = getCookie(setCookies, '_shopify_essential')

if (!shopifyEssential) {
outputDebug(
`Failed to obtain _shopify_essential cookie.\n
-Request ID: ${response.headers.get('x-request-id') ?? 'unknown'}\n
-Body: ${await response.text()}`,
)
}

return shopifyEssential
}

Expand All @@ -129,6 +138,14 @@ async function enrichSessionWithStorefrontPassword(
const setCookies = response.headers.raw()['set-cookie'] ?? []
const storefrontDigest = getCookie(setCookies, 'storefront_digest')

if (!storefrontDigest) {
outputDebug(
`Failed to obtain storefront_digest cookie.\n
-Request ID: ${response.headers.get('x-request-id') ?? 'unknown'}\n
-Body: ${await response.text()}`,
)
}

return storefrontDigest
}

Expand Down

0 comments on commit 23daedb

Please sign in to comment.