Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(templates): broken preview if alternative auth strategy was used, invalid error handling #9785

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/draft-preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"dotenv": "^8.2.0",
"escape-html": "^1.0.3",
"graphql": "^16.9.0",
"jsonwebtoken": "9.0.2",
"next": "^15.0.0",
"payload": "latest",
"payload-admin-bar": "^1.0.6",
Expand Down
21 changes: 9 additions & 12 deletions examples/draft-preview/src/app/(app)/next/preview/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { CollectionSlug } from 'payload'
import type { CollectionSlug, PayloadRequest } from 'payload'

import jwt from 'jsonwebtoken'
import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'
import { getPayload } from 'payload'
Expand Down Expand Up @@ -42,23 +41,21 @@ export async function GET(
return new Response('No path provided', { status: 404 })
}

if (!token) {
new Response('You are not allowed to preview this page', { status: 403 })
}

if (!path.startsWith('/')) {
new Response('This endpoint can only be used for internal previews', { status: 500 })
return new Response('This endpoint can only be used for internal previews', { status: 500 })
}

let user

try {
user = jwt.verify(token, payload.secret)
} catch (error) {
payload.logger.error({
err: error,
msg: 'Error verifying token for live preview',
user = await payload.auth({
req: req as unknown as PayloadRequest,
headers: req.headers,
})
} catch (error) {
console.log({ token, payloadSecret: payload.secret })
payload.logger.error({ err: error }, 'Error verifying token for live preview')
return new Response('You are not allowed to preview this page', { status: 403 })
}

const draft = await draftMode()
Expand Down
2 changes: 0 additions & 2 deletions templates/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"cross-env": "^7.0.3",
"geist": "^1.3.0",
"graphql": "^16.8.2",
"jsonwebtoken": "9.0.2",
"lucide-react": "^0.378.0",
"next": "^15.0.3",
"next-sitemap": "^4.2.3",
Expand All @@ -58,7 +57,6 @@
"@payloadcms/eslint-config": "^1.1.1",
"@tailwindcss/typography": "^0.5.13",
"@types/escape-html": "^1.0.2",
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "22.5.4",
"@types/react": "npm:types-react@19.0.0-rc.1",
"@types/react-dom": "npm:types-react-dom@19.0.0-rc.1",
Expand Down
20 changes: 10 additions & 10 deletions templates/website/src/app/(frontend)/next/preview/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import jwt from 'jsonwebtoken'
import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'
import { getPayload } from 'payload'
import { getPayload, type PayloadRequest } from 'payload'
import configPromise from '@payload-config'
import { CollectionSlug } from 'payload'

Expand Down Expand Up @@ -40,20 +39,21 @@ export async function GET(
return new Response('No path provided', { status: 404 })
}

if (!token) {
new Response('You are not allowed to preview this page', { status: 403 })
}

if (!path.startsWith('/')) {
new Response('This endpoint can only be used for internal previews', { status: 500 })
return new Response('This endpoint can only be used for internal previews', { status: 500 })
}

let user

try {
user = jwt.verify(token, payload.secret)
user = await payload.auth({
req: req as unknown as PayloadRequest,
headers: req.headers,
})
} catch (error) {
payload.logger.error('Error verifying token for live preview:', error)
console.log({ token, payloadSecret: payload.secret })
payload.logger.error({ err: error }, 'Error verifying token for live preview')
return new Response('You are not allowed to preview this page', { status: 403 })
}

const draft = await draftMode()
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function GET(
return new Response('Document not found', { status: 404 })
}
} catch (error) {
payload.logger.error('Error verifying token for live preview:', error)
payload.logger.error({ err: error }, 'Error verifying token for live preview')
}

draft.enable()
Expand Down
2 changes: 0 additions & 2 deletions templates/with-vercel-website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"cross-env": "^7.0.3",
"geist": "^1.3.0",
"graphql": "^16.8.2",
"jsonwebtoken": "9.0.2",
"lucide-react": "^0.378.0",
"next": "^15.0.3",
"next-sitemap": "^4.2.3",
Expand All @@ -60,7 +59,6 @@
"@payloadcms/eslint-config": "^1.1.1",
"@tailwindcss/typography": "^0.5.13",
"@types/escape-html": "^1.0.2",
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "22.5.4",
"@types/react": "npm:types-react@19.0.0-rc.1",
"@types/react-dom": "npm:types-react-dom@19.0.0-rc.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import jwt from 'jsonwebtoken'
import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'
import { getPayload } from 'payload'
import configPromise from '@payload-config'
import { CollectionSlug } from 'payload'
import type { CollectionSlug, PayloadRequest } from 'payload'

const payloadToken = 'payload-token'

Expand Down Expand Up @@ -40,20 +39,21 @@ export async function GET(
return new Response('No path provided', { status: 404 })
}

if (!token) {
new Response('You are not allowed to preview this page', { status: 403 })
}

if (!path.startsWith('/')) {
new Response('This endpoint can only be used for internal previews', { status: 500 })
return new Response('This endpoint can only be used for internal previews', { status: 500 })
}

let user

try {
user = jwt.verify(token, payload.secret)
user = await payload.auth({
req: req as unknown as PayloadRequest,
headers: req.headers,
})
} catch (error) {
payload.logger.error('Error verifying token for live preview:', error)
console.log({ token, payloadSecret: payload.secret })
payload.logger.error({ err: error }, 'Error verifying token for live preview')
return new Response('You are not allowed to preview this page', { status: 403 })
}

const draft = await draftMode()
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function GET(
return new Response('Document not found', { status: 404 })
}
} catch (error) {
payload.logger.error('Error verifying token for live preview:', error)
payload.logger.error({ err: error }, 'Error verifying token for live preview')
}

draft.enable()
Expand Down
Loading