Skip to content

Commit

Permalink
fix(basic-starter): fetch resource with auth in Draft Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
balintbrews committed Jun 11, 2024
1 parent f56c243 commit 5b6cc0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion examples/example-router-migration/app/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ async function getNode(slug: string[]) {

const params: JsonApiParams = {}

const isDraftMode = draftMode().isEnabled
const draftData = getDraftData()

if (draftData.path === path) {
params.resourceVersion = draftData.resourceVersion
}

// Translating the path also allows us to discover the entity type.
const translatedPath = await drupal.translatePath(path)
const translatedPath = await drupal.translatePath(path, {
withAuth: isDraftMode,
})

if (!translatedPath) {
throw new Error("Resource not found", { cause: "NotFound" })
Expand All @@ -34,6 +37,7 @@ async function getNode(slug: string[]) {

const resource = await drupal.getResource<DrupalNode>(type, uuid, {
params,
withAuth: isDraftMode,
})

if (!resource) {
Expand Down
6 changes: 5 additions & 1 deletion starters/basic-starter/app/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ async function getNode(slug: string[]) {

const params: JsonApiParams = {}

const isDraftMode = draftMode().isEnabled;
const draftData = getDraftData()

if (draftData.path === path) {
params.resourceVersion = draftData.resourceVersion
}

// Translating the path also allows us to discover the entity type.
const translatedPath = await drupal.translatePath(path)
const translatedPath = await drupal.translatePath(path, {
withAuth: isDraftMode,
})

if (!translatedPath) {
throw new Error("Resource not found", { cause: "NotFound" })
Expand All @@ -34,6 +37,7 @@ async function getNode(slug: string[]) {

const resource = await drupal.getResource<DrupalNode>(type, uuid, {
params,
withAuth: isDraftMode,
})

if (!resource) {
Expand Down

0 comments on commit 5b6cc0f

Please sign in to comment.