feat: add login and print user object on my-pages
This commit is contained in:
committed by
Simon Emanuelsson
parent
f1278a8d11
commit
70f9c22410
@@ -1,42 +1,63 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import type { NextRequest } from "next/server"
|
||||
|
||||
// This function can be marked `async` if using `await` inside
|
||||
export async function middleware(request: NextRequest) {
|
||||
// const locales = await fetch(CMS_API, {
|
||||
// locales: true
|
||||
// })
|
||||
const locales = ["en", "sv", "no", "fi", "da", "de"]
|
||||
import { auth } from "@/auth"
|
||||
|
||||
const locale = locales.find(
|
||||
(locale) =>
|
||||
request.nextUrl.pathname.startsWith(`/${locale}/`) ||
|
||||
request.nextUrl.pathname === `/${locale}`
|
||||
)
|
||||
import { findLocale } from "@/constants/locales"
|
||||
import { pageNames } from "@/constants/myPages"
|
||||
|
||||
import { apiAuthPrefix } from "@/routes/api"
|
||||
// import { publicRoutes } from "@/routes/public"
|
||||
import { protectedRoutes } from "@/routes/protected"
|
||||
|
||||
export default auth(async function middleware(request) {
|
||||
const { nextUrl } = request
|
||||
const isLoggedIn = !!request.auth
|
||||
|
||||
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 data = await fetch(CMS_API, {
|
||||
// uri: request.nextUrl.pathname,
|
||||
// locale
|
||||
// }).json()
|
||||
const isProtectedRoute = protectedRoutes.includes(nextUrl.pathname)
|
||||
if (isProtectedRoute) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
if (nextUrl.pathname.startsWith(`/${locale}/login`)) {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
// const isPublicRoute = publicRoutes.includes(nextUrl.pathname)
|
||||
// if (!isLoggedIn && !isPublicRoute) {
|
||||
// return NextResponse.redirect(new URL(`/${locale}/login`, nextUrl))
|
||||
// }
|
||||
|
||||
//const contentType = data.response.meta.contentType;
|
||||
const contentType = "currentContentPage"
|
||||
|
||||
const pathNameWithoutLocale = request.nextUrl.pathname.replace(
|
||||
`/${locale}`,
|
||||
""
|
||||
)
|
||||
|
||||
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()}`,
|
||||
@@ -46,7 +67,6 @@ export async function middleware(request: NextRequest) {
|
||||
}
|
||||
|
||||
searchParams.set("uri", pathNameWithoutLocale)
|
||||
|
||||
switch (contentType) {
|
||||
case "currentContentPage":
|
||||
return NextResponse.rewrite(
|
||||
@@ -56,19 +76,16 @@ export async function middleware(request: NextRequest) {
|
||||
)
|
||||
)
|
||||
}
|
||||
return NextResponse.redirect(new URL("/home", request.url))
|
||||
}
|
||||
|
||||
return NextResponse.next()
|
||||
})
|
||||
|
||||
// See "Matching Paths" below to learn more
|
||||
export const config = {
|
||||
matcher: [
|
||||
/*
|
||||
* Match all request paths except for the ones starting with:
|
||||
* - api (API routes)
|
||||
* - _next/static (static files)
|
||||
* - _next/image (image optimization files)
|
||||
* - favicon.ico (favicon file)
|
||||
*/
|
||||
"/((?!api|_next/static|_next/image|_static|imageVault|contentassets|favicon.ico|en/test).*)",
|
||||
],
|
||||
/**
|
||||
* 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)(.*)"],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user