feat(WEB-132): add middlewares, support for seamless login and improve lang based routes
This commit is contained in:
115
middleware.ts
115
middleware.ts
@@ -1,101 +1,28 @@
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { NextMiddleware } from "next/server"
|
||||
|
||||
import { auth } from "@/auth"
|
||||
import * as handleAuth from "./middlewares/handleAuth"
|
||||
import * as authRequired from "./middlewares/authRequired"
|
||||
import * as currentWebLogin from "./middlewares/currentWebLogin"
|
||||
import * as ensureLang from "./middlewares/ensureLang"
|
||||
import * as cmsContent from "@/middlewares/cmsContent"
|
||||
import * as webView from "@/middlewares/webView"
|
||||
|
||||
import { findLang } from "@/constants/languages"
|
||||
import { pageNames } from "@/constants/myPages"
|
||||
export const middleware: NextMiddleware = async (request, event) => {
|
||||
const middlewares = [
|
||||
ensureLang,
|
||||
currentWebLogin,
|
||||
authRequired,
|
||||
handleAuth,
|
||||
webView,
|
||||
cmsContent,
|
||||
]
|
||||
|
||||
import { protectedRoutes } from "@/routes/protected"
|
||||
for (let i = 0; i < middlewares.length; ++i) {
|
||||
const middleware = middlewares[i]
|
||||
|
||||
import type { NextAuthRequest } from "next-auth"
|
||||
|
||||
export async function publiceMiddleware(request: NextRequest) {
|
||||
const { nextUrl } = request
|
||||
const lang = findLang(nextUrl.pathname)
|
||||
|
||||
if (nextUrl.pathname.startsWith(`/${lang}/login`)) {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
const contentType = "currentContentPage"
|
||||
const pathNameWithoutLang = nextUrl.pathname.replace(`/${lang}`, "")
|
||||
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
|
||||
|
||||
if (request.nextUrl.pathname.includes("preview")) {
|
||||
searchParams.set("uri", pathNameWithoutLang.replace("/preview", ""))
|
||||
return NextResponse.rewrite(
|
||||
new URL(`/${lang}/preview-current?${searchParams.toString()}`, nextUrl)
|
||||
)
|
||||
}
|
||||
|
||||
searchParams.set("uri", pathNameWithoutLang)
|
||||
switch (contentType) {
|
||||
case "currentContentPage":
|
||||
return NextResponse.rewrite(
|
||||
new URL(
|
||||
`/${lang}/current-content-page?${searchParams.toString()}`,
|
||||
nextUrl
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
// Unreachable atm
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
async function authedMiddlewareFunction(request: NextAuthRequest) {
|
||||
const { nextUrl } = request
|
||||
const lang = findLang(nextUrl.pathname)!
|
||||
const isLoggedIn = !!request.auth
|
||||
if (isLoggedIn) {
|
||||
/**
|
||||
* Temporary hard rewrite to my pages
|
||||
*/
|
||||
return NextResponse.rewrite(new URL(`/${lang}/${pageNames[lang]}`, nextUrl))
|
||||
} else {
|
||||
/**
|
||||
* Redirect to Loginpage
|
||||
* (Loginpage most likely to be removed)
|
||||
*/
|
||||
return NextResponse.redirect(new URL(`/${lang}/login`, nextUrl))
|
||||
}
|
||||
}
|
||||
|
||||
const authedMiddleware = auth(authedMiddlewareFunction)
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
const { nextUrl } = request
|
||||
|
||||
const lang = findLang(nextUrl.pathname)
|
||||
if (!lang) {
|
||||
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. This is only done for Route handlers (route.js) and
|
||||
* not for middleware.
|
||||
*
|
||||
* Auth.js uses the same pattern for both Route handlers and Middleware,
|
||||
* the auth()-wrapper:
|
||||
*
|
||||
* auth((req) => { ... })
|
||||
*
|
||||
* But there is a difference between middleware and route handlers, route
|
||||
* handlers get passed a context which middleware do not get. Using the
|
||||
* same function for both works runtime because second argument is just
|
||||
* undefined for middleware and Auth.js handles this properly. But fails in
|
||||
* typings as the second argument doesn't exist for middleware.
|
||||
*
|
||||
* https://github.com/nextauthjs/next-auth/blob/3c035ec62f2f21d7cab65504ba83fb1a9a13be01/packages/next-auth/src/lib/index.ts#L265
|
||||
* https://authjs.dev/reference/nextjs
|
||||
*/
|
||||
// @ts-expect-error: see above
|
||||
return authedMiddleware(request)
|
||||
} else {
|
||||
return publiceMiddleware(request)
|
||||
if (middleware.matcher(request)) {
|
||||
return await middleware.middleware(request, event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user