Merged in fix/rewrite-nexturl (pull request #73)
fix: split middleware in two, authed and public Approved-by: Michael Zetterberg
This commit is contained in:
128
middleware.ts
128
middleware.ts
@@ -1,4 +1,4 @@
|
|||||||
import { NextResponse } from "next/server"
|
import { NextRequest, NextResponse } from "next/server"
|
||||||
|
|
||||||
import { auth } from "@/auth"
|
import { auth } from "@/auth"
|
||||||
|
|
||||||
@@ -6,12 +6,68 @@ import { findLocale } from "@/constants/locales"
|
|||||||
import { pageNames } from "@/constants/myPages"
|
import { pageNames } from "@/constants/myPages"
|
||||||
|
|
||||||
import { apiAuthPrefix } from "@/routes/api"
|
import { apiAuthPrefix } from "@/routes/api"
|
||||||
// import { publicRoutes } from "@/routes/public"
|
|
||||||
import { protectedRoutes } from "@/routes/protected"
|
import { protectedRoutes } from "@/routes/protected"
|
||||||
|
|
||||||
export default auth(async function middleware(request) {
|
import type { NextAuthRequest } from "next-auth"
|
||||||
|
|
||||||
|
export async function publiceMiddleware(request: NextRequest) {
|
||||||
const { nextUrl } = request
|
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
|
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)
|
const isApiRoute = nextUrl.pathname.startsWith(apiAuthPrefix)
|
||||||
if (isApiRoute) {
|
if (isApiRoute) {
|
||||||
@@ -26,59 +82,21 @@ export default auth(async function middleware(request) {
|
|||||||
|
|
||||||
const isProtectedRoute = protectedRoutes.includes(nextUrl.pathname)
|
const isProtectedRoute = protectedRoutes.includes(nextUrl.pathname)
|
||||||
if (isProtectedRoute) {
|
if (isProtectedRoute) {
|
||||||
if (isLoggedIn) {
|
/**
|
||||||
/**
|
* AppRouteHandlerFnContext is the context that is passed to the handler as the
|
||||||
* Temporary hard rewrite to my pages
|
* second argument.
|
||||||
*/
|
*
|
||||||
return NextResponse.rewrite(
|
* type AppRouteHandlerFnContext = {
|
||||||
new URL(`/${locale}/${pageNames[locale]}`, nextUrl)
|
* params?: Record<string, string | string[]>
|
||||||
)
|
* }
|
||||||
} else {
|
*
|
||||||
/**
|
* We don't need it so just pass an empty object
|
||||||
* Redirect to Loginpage
|
*/
|
||||||
* (Loginpage most likely to be removed)
|
return authedMiddleware(request, {})
|
||||||
*/
|
} else {
|
||||||
return NextResponse.redirect(new URL(`/${locale}/login`, nextUrl))
|
return publiceMiddleware(request)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
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 = 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()}`,
|
|
||||||
request.url
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
searchParams.set("uri", pathNameWithoutLocale)
|
|
||||||
switch (contentType) {
|
|
||||||
case "currentContentPage":
|
|
||||||
return NextResponse.rewrite(
|
|
||||||
new URL(
|
|
||||||
`/${locale}/current-content-page?${searchParams.toString()}`,
|
|
||||||
request.url
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.next()
|
|
||||||
})
|
|
||||||
|
|
||||||
// See "Matching Paths" below to learn more
|
// See "Matching Paths" below to learn more
|
||||||
export const config = {
|
export const config = {
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
/* Unauthenticated routes */
|
|
||||||
export const publicRoutes: string[] = ["/"]
|
|
||||||
9
types/auth.d.ts
vendored
9
types/auth.d.ts
vendored
@@ -1,4 +1,5 @@
|
|||||||
import "next-auth"
|
import "next-auth"
|
||||||
|
import type { NextRequest } from "next/server"
|
||||||
|
|
||||||
// Module augmentation
|
// Module augmentation
|
||||||
// https://authjs.dev/getting-started/typescript#popular-interfaces-to-augment
|
// https://authjs.dev/getting-started/typescript#popular-interfaces-to-augment
|
||||||
@@ -21,6 +22,14 @@ declare module "next-auth" {
|
|||||||
* Returned by `useSession`, `auth`, contains information about the active session.
|
* Returned by `useSession`, `auth`, contains information about the active session.
|
||||||
*/
|
*/
|
||||||
interface Session {}
|
interface Session {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NextAuthRequest isn't exported by next-auth so we declare a copy
|
||||||
|
* of how they do it to support or switch in middleware.ts
|
||||||
|
*/
|
||||||
|
interface NextAuthRequest extends NextRequest {
|
||||||
|
auth: Session | null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module "next-auth/jwt" {
|
declare module "next-auth/jwt" {
|
||||||
|
|||||||
Reference in New Issue
Block a user