From 44c54c5b90e78bae41ab1c972d4280a0faa8ba56 Mon Sep 17 00:00:00 2001 From: Mikkel Jakobsen Date: Thu, 12 Dec 2024 17:27:53 +0100 Subject: [PATCH] Fix some async API changes in NextJs 15 For some reason these problems was not caught by our ci checks. --- app/auth/token/refresh/route.ts | 13 +++++++++---- app/work/[id]/read/page.tsx | 11 ++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/auth/token/refresh/route.ts b/app/auth/token/refresh/route.ts index 26d57980..13671aaa 100644 --- a/app/auth/token/refresh/route.ts +++ b/app/auth/token/refresh/route.ts @@ -1,3 +1,4 @@ +import { headers } from "next/headers" import { NextRequest, NextResponse } from "next/server" import * as client from "openid-client" import { z } from "zod" @@ -13,7 +14,11 @@ const sessionTokenSchema = z.object({ refresh_token: z.string(), }) -export async function GET(request: NextRequest, response: NextResponse) { +// TODO: check if headers still work as intended, +// when reenabling this together with refresh token functionality in the middleware. +export async function GET(request: NextRequest) { + const requestHeaders = await headers() + const appUrl = String(goConfig("app.url")) const config = await getUniloginClientConfig() // TODO: Fix refresh token flow with new openid-client. @@ -23,12 +28,12 @@ export async function GET(request: NextRequest, response: NextResponse) { // If the user is not logged in, we redirect to the frontpage. if (!session.isLoggedIn) { - return NextResponse.redirect(frontpage, { headers: response.headers }) + return NextResponse.redirect(frontpage, { headers: requestHeaders }) } const redirect = request.nextUrl.searchParams.get("redirect") // We need the redirect URL to be present in the query string. if (!redirect) { - return NextResponse.redirect(frontpage, { headers: response.headers }) + return NextResponse.redirect(frontpage, { headers: requestHeaders }) } try { @@ -46,7 +51,7 @@ export async function GET(request: NextRequest, response: NextResponse) { const isZodError = error instanceof z.ZodError console.error(isZodError ? JSON.stringify(error.errors) : error) } finally { - return NextResponse.redirect(redirect, { headers: response.headers }) + return NextResponse.redirect(redirect, { headers: requestHeaders }) } } diff --git a/app/work/[id]/read/page.tsx b/app/work/[id]/read/page.tsx index 2f261079..620dae3b 100644 --- a/app/work/[id]/read/page.tsx +++ b/app/work/[id]/read/page.tsx @@ -1,14 +1,23 @@ "use client" +import { useSearchParams } from "next/navigation" import React from "react" import Reader from "@/components/shared/publizonReader/PublizonReader" -function Page({ searchParams: { id } }: { searchParams: { id: string } }) { +function Page() { + const searchParams = useSearchParams() + const id = searchParams.get("id") + const handleBack = () => { window.history.back() } + // TODO: Do we want to do this if there is no id? + if (!id) { + return null + } + return (