-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d06753
commit 303ddf6
Showing
6 changed files
with
97 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
import { sitePreviewRoute } from "@comet/cms-site"; | ||
import { sitePreviewRoute } from "@src/temp/site-preview-route"; | ||
import { createGraphQLFetch } from "@src/util/graphQLClient"; | ||
import { type NextRequest } from "next/server"; | ||
|
||
export const dynamic = "force-dynamic"; | ||
|
||
export async function GET(request: NextRequest) { | ||
return sitePreviewRoute(request, createGraphQLFetch()); | ||
return sitePreviewRoute(request, createGraphQLFetch(), { | ||
basePath: (request) => { | ||
const scopeString = request.nextUrl.searchParams.get("scope"); | ||
|
||
if (scopeString === null) { | ||
throw new Error("Missing scope param"); | ||
} | ||
|
||
const scope: { language?: string } = JSON.parse(scopeString); | ||
return scope.language ?? ""; | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Temporary copy from | ||
|
||
import { GraphQLFetch } from "@comet/cms-site"; | ||
import { SignJWT } from "jose"; | ||
import { cookies, draftMode } from "next/headers"; | ||
import { redirect } from "next/navigation"; | ||
import { type NextRequest } from "next/server"; | ||
import * as path from "path"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type Scope = Record<string, any>; | ||
|
||
export type SitePreviewData = { | ||
includeInvisible: boolean; | ||
}; | ||
export type SitePreviewParams = { | ||
scope: Scope; | ||
previewData?: SitePreviewData; | ||
}; | ||
|
||
function getPreviewScopeSigningKey() { | ||
if (!process.env.SITE_PREVIEW_SECRET && process.env.NODE_ENV === "production") { | ||
throw new Error("SITE_PREVIEW_SECRET environment variable is required in production mode"); | ||
} | ||
return process.env.SITE_PREVIEW_SECRET || "secret"; | ||
} | ||
|
||
export async function sitePreviewRoute(request: NextRequest, graphQLFetch: GraphQLFetch, options?: { basePath?: (request: NextRequest) => string }) { | ||
const previewScopeSigningKey = getPreviewScopeSigningKey(); | ||
const params = request.nextUrl.searchParams; | ||
const settingsParam = params.get("settings"); | ||
const scopeParam = params.get("scope"); | ||
if (!settingsParam || !scopeParam) { | ||
throw new Error("Missing settings or scope parameter"); | ||
} | ||
|
||
const previewData = JSON.parse(settingsParam); | ||
const scope = JSON.parse(scopeParam); | ||
|
||
const { currentUser } = await graphQLFetch<{ currentUser: { permissionsForScope: string[] } }, { scope: Scope }>( | ||
` | ||
query CurrentUserPermissionsForScope($scope: JSONObject!) { | ||
currentUser { | ||
permissionsForScope(scope: $scope) | ||
} | ||
} | ||
`, | ||
{ scope }, | ||
{ | ||
headers: { | ||
authorization: request.headers.get("authorization") || "", | ||
}, | ||
}, | ||
); | ||
if (!currentUser.permissionsForScope.includes("pageTree")) { | ||
return new Response("Preview is not allowed", { | ||
status: 403, | ||
}); | ||
} | ||
|
||
const data: SitePreviewParams = { scope, previewData }; | ||
const token = await new SignJWT(data).setProtectedHeader({ alg: "HS256" }).sign(new TextEncoder().encode(previewScopeSigningKey)); | ||
cookies().set("__comet_preview", token); | ||
|
||
draftMode().enable(); | ||
|
||
return redirect(`/${path.join(options?.basePath?.(request) ?? "", params.get("path") ?? "")}` || "/"); | ||
} |