feat: rename use of locale to lang where applicable

This commit is contained in:
Michael Zetterberg
2024-04-08 15:17:07 +02:00
parent f3c9386ef4
commit 7ebc604156
14 changed files with 110 additions and 118 deletions

View File

@@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from "next/server"
import { auth } from "@/auth"
import { findLocale } from "@/constants/locales"
import { findLang } from "@/constants/languages"
import { pageNames } from "@/constants/myPages"
import { protectedRoutes } from "@/routes/protected"
@@ -11,29 +11,29 @@ import type { NextAuthRequest } from "next-auth"
export async function publiceMiddleware(request: NextRequest) {
const { nextUrl } = request
const locale = findLocale(nextUrl.pathname)
const lang = findLang(nextUrl.pathname)
if (nextUrl.pathname.startsWith(`/${locale}/login`)) {
if (nextUrl.pathname.startsWith(`/${lang}/login`)) {
return NextResponse.next()
}
const contentType = "currentContentPage"
const pathNameWithoutLocale = nextUrl.pathname.replace(`/${locale}`, "")
const pathNameWithoutLang = nextUrl.pathname.replace(`/${lang}`, "")
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
if (request.nextUrl.pathname.includes("preview")) {
searchParams.set("uri", pathNameWithoutLocale.replace("/preview", ""))
searchParams.set("uri", pathNameWithoutLang.replace("/preview", ""))
return NextResponse.rewrite(
new URL(`/${locale}/preview-current?${searchParams.toString()}`, nextUrl)
new URL(`/${lang}/preview-current?${searchParams.toString()}`, nextUrl)
)
}
searchParams.set("uri", pathNameWithoutLocale)
searchParams.set("uri", pathNameWithoutLang)
switch (contentType) {
case "currentContentPage":
return NextResponse.rewrite(
new URL(
`/${locale}/current-content-page?${searchParams.toString()}`,
`/${lang}/current-content-page?${searchParams.toString()}`,
nextUrl
)
)
@@ -45,21 +45,19 @@ export async function publiceMiddleware(request: NextRequest) {
async function authedMiddlewareFunction(request: NextAuthRequest) {
const { nextUrl } = request
const locale = findLocale(nextUrl.pathname)!
const lang = findLang(nextUrl.pathname)!
const isLoggedIn = !!request.auth
if (isLoggedIn) {
/**
* Temporary hard rewrite to my pages
*/
return NextResponse.rewrite(
new URL(`/${locale}/${pageNames[locale]}`, nextUrl)
)
return NextResponse.rewrite(new URL(`/${lang}/${pageNames[lang]}`, nextUrl))
} else {
/**
* Redirect to Loginpage
* (Loginpage most likely to be removed)
*/
return NextResponse.redirect(new URL(`/${locale}/login`, nextUrl))
return NextResponse.redirect(new URL(`/${lang}/login`, nextUrl))
}
}
@@ -68,9 +66,8 @@ const authedMiddleware = auth(authedMiddlewareFunction)
export async function middleware(request: NextRequest) {
const { nextUrl } = request
const locale = findLocale(nextUrl.pathname)
if (!locale) {
//return <LocalePicker />
const lang = findLang(nextUrl.pathname)
if (!lang) {
return Response.json("Not found!!!", { status: 404 })
}