refactor: make my-pages page more reusable

This commit is contained in:
Arvid Norlin
2024-04-30 14:48:20 +02:00
parent a1e474073e
commit a81f641ccd
9 changed files with 44 additions and 137 deletions

View File

@@ -3,6 +3,7 @@ import { NextResponse } from "next/server"
import { findLang } from "@/constants/languages"
import { authRequired } from "@/constants/routes/authRequired"
import { login } from "@/constants/routes/handleAuth"
import { benefits, myPages, overview } from "@/constants/routes/myPages"
import { auth } from "@/auth"
@@ -42,6 +43,18 @@ export const middleware = auth(async (request) => {
const isLoggedIn = !!request.auth
if (isLoggedIn) {
const pathNameWithoutLang = nextUrl.pathname.replace(`/${lang}`, "")
// Temp fix until we have a better solution for identifying AccountPage type
const accountPagePaths = [myPages[lang], overview[lang], benefits[lang]]
if (accountPagePaths.includes(nextUrl.pathname)) {
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
searchParams.set("uri", pathNameWithoutLang)
return NextResponse.rewrite(
new URL(`/${lang}/my-pages?${searchParams.toString()}`, nextUrl)
)
}
return NextResponse.next()
}