added tons of features

This commit is contained in:
2025-02-05 08:01:14 +02:00
parent 4ae0d8c545
commit 8138da6b1d
195 changed files with 12619 additions and 415 deletions

36
middlewares/chain.ts Normal file
View File

@@ -0,0 +1,36 @@
/***************************************
*
* Based on
* https://medium.com/@tanzimhossain2/implementing-multiple-middleware-in-next-js-combining-nextauth-and-internationalization-28d5435d3187
*
**************************************/
import {NextMiddlewareResult} from 'next/dist/server/web/types'
import {NextResponse} from 'next/server'
import type {NextFetchEvent, NextRequest} from 'next/server'
export type CustomMiddleware = (
request: NextRequest,
event: NextFetchEvent,
response: NextResponse
) => NextMiddlewareResult | Promise<NextMiddlewareResult>
type MiddlewareFactory = (middleware: CustomMiddleware) => CustomMiddleware
export function chain(
functions: MiddlewareFactory[],
index = 0
): CustomMiddleware {
const current = functions[index]
if (current) {
const next = chain(functions, index + 1)
return current(next)
}
return (
request: NextRequest,
event: NextFetchEvent,
response: NextResponse
) => {
return response
}
}

View File

@@ -0,0 +1,18 @@
import {type NextMiddlewareResult} from 'next/dist/server/web/types'
import type {NextFetchEvent, NextRequest} from 'next/server'
import {NextResponse} from 'next/server'
import {CustomMiddleware} from './chain'
import {auth} from '@/auth'
export function withAuthMiddleware(
middleware: CustomMiddleware
): CustomMiddleware {
return async (
request: NextRequest,
event: NextFetchEvent,
response: NextResponse
): Promise<NextMiddlewareResult> => {
return middleware(request, event, response)
}
}

View File

@@ -0,0 +1,25 @@
import createMiddleware from 'next-intl/middleware'
import {NextFetchEvent, NextRequest, NextResponse} from 'next/server'
import {routing} from '@/i18n/routing'
import {HEADERS} from '@/lib/config/http'
import {translatableRoutesRegEx} from '@/lib/config/routes'
import {type CustomMiddleware} from '@/middlewares/chain'
export function withI18nMiddleware(middleware: CustomMiddleware) {
return async (
request: NextRequest,
event: NextFetchEvent,
response: NextResponse
) => {
if (request.nextUrl.pathname.match(translatableRoutesRegEx) !== null) {
response = createMiddleware(routing)(request)
}
let locale =
request.cookies.get('NEXT_LOCALE')?.value ?? routing.defaultLocale
response.headers.set(HEADERS.xSiteLocale, locale)
return middleware(request, event, response)
}
}