110 lines
3.1 KiB
TypeScript
110 lines
3.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server"
|
|
|
|
import { auth } from "@/auth"
|
|
|
|
import { findLocale } from "@/constants/locales"
|
|
import { pageNames } from "@/constants/myPages"
|
|
|
|
import { apiAuthPrefix } from "@/routes/api"
|
|
import { protectedRoutes } from "@/routes/protected"
|
|
|
|
import type { NextAuthRequest } from "next-auth"
|
|
|
|
export async function publiceMiddleware(request: NextRequest) {
|
|
const { nextUrl } = request
|
|
const locale = findLocale(nextUrl.pathname)
|
|
|
|
if (nextUrl.pathname.startsWith(`/${locale}/login`)) {
|
|
return NextResponse.next()
|
|
}
|
|
|
|
const contentType = "currentContentPage"
|
|
const pathNameWithoutLocale = nextUrl.pathname.replace(`/${locale}`, "")
|
|
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
|
|
|
|
if (request.nextUrl.pathname.includes("preview")) {
|
|
searchParams.set("uri", pathNameWithoutLocale.replace("/preview", ""))
|
|
return NextResponse.rewrite(
|
|
new URL(`/${locale}/preview-current?${searchParams.toString()}`, nextUrl)
|
|
)
|
|
}
|
|
|
|
searchParams.set("uri", pathNameWithoutLocale)
|
|
switch (contentType) {
|
|
case "currentContentPage":
|
|
return NextResponse.rewrite(
|
|
new URL(
|
|
`/${locale}/current-content-page?${searchParams.toString()}`,
|
|
nextUrl
|
|
)
|
|
)
|
|
}
|
|
|
|
// Unreachable atm
|
|
return NextResponse.next()
|
|
}
|
|
|
|
async function authedMiddlewareFunction(request: NextAuthRequest) {
|
|
const { nextUrl } = request
|
|
const locale = findLocale(nextUrl.pathname)!
|
|
const isLoggedIn = !!request.auth
|
|
if (isLoggedIn) {
|
|
/**
|
|
* Temporary hard rewrite to my pages
|
|
*/
|
|
return NextResponse.rewrite(
|
|
new URL(`/${locale}/${pageNames[locale]}`, nextUrl)
|
|
)
|
|
} else {
|
|
/**
|
|
* Redirect to Loginpage
|
|
* (Loginpage most likely to be removed)
|
|
*/
|
|
return NextResponse.redirect(new URL(`/${locale}/login`, nextUrl))
|
|
}
|
|
}
|
|
|
|
const authedMiddleware = auth(authedMiddlewareFunction)
|
|
|
|
export async function middleware(request: NextRequest) {
|
|
const { nextUrl } = request
|
|
|
|
const isApiRoute = nextUrl.pathname.startsWith(apiAuthPrefix)
|
|
if (isApiRoute) {
|
|
return NextResponse.next()
|
|
}
|
|
|
|
const locale = findLocale(nextUrl.pathname)
|
|
if (!locale) {
|
|
//return <LocalePicker />
|
|
return Response.json("Not found!!!", { status: 404 })
|
|
}
|
|
|
|
const isProtectedRoute = protectedRoutes.includes(nextUrl.pathname)
|
|
if (isProtectedRoute) {
|
|
/**
|
|
* AppRouteHandlerFnContext is the context that is passed to the handler as the
|
|
* second argument.
|
|
*
|
|
* type AppRouteHandlerFnContext = {
|
|
* params?: Record<string, string | string[]>
|
|
* }
|
|
*
|
|
* We don't need it so just pass an empty object
|
|
*/
|
|
return authedMiddleware(request, {})
|
|
} else {
|
|
return publiceMiddleware(request)
|
|
}
|
|
}
|
|
|
|
// See "Matching Paths" below to learn more
|
|
export const config = {
|
|
/**
|
|
* Copied from Clerk to protect all routes by default and handle
|
|
* public routes inside middleware.
|
|
* (https://clerk.com/docs/quickstarts/nextjs?utm_source=sponsorship&utm_medium=youtube&utm_campaign=code-with-antonio&utm_content=12-31-2023#add-authentication-to-your-app)
|
|
*/
|
|
matcher: ["/((?!.+\\.[\\w]+$|_next|en/test).*)", "/", "/(api)(.*)"],
|
|
}
|