mirror of
https://github.com/beilunyang/moemail.git
synced 2026-07-12 16:11:20 +08:00
feat: add internationalization support with next-intl
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { auth } from "@/lib/auth"
|
||||
import { NextResponse } from "next/server"
|
||||
import { i18n } from "@/i18n/config"
|
||||
import { PERMISSIONS } from "@/lib/permissions"
|
||||
import { checkPermission } from "@/lib/auth"
|
||||
import { Permission } from "@/lib/permissions"
|
||||
@@ -14,40 +15,57 @@ const API_PERMISSIONS: Record<string, Permission> = {
|
||||
}
|
||||
|
||||
export async function middleware(request: Request) {
|
||||
const pathname = new URL(request.url).pathname
|
||||
const url = new URL(request.url)
|
||||
const pathname = url.pathname
|
||||
|
||||
// API Key 认证
|
||||
request.headers.delete("X-User-Id")
|
||||
const apiKey = request.headers.get("X-API-Key")
|
||||
if (apiKey) {
|
||||
return handleApiKeyAuth(apiKey, pathname)
|
||||
}
|
||||
if (pathname.startsWith('/api')) {
|
||||
if (pathname.startsWith('/api/auth')) {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
// Session 认证
|
||||
const session = await auth()
|
||||
if (!session?.user) {
|
||||
return NextResponse.json(
|
||||
{ error: "未授权" },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
request.headers.delete("X-User-Id")
|
||||
const apiKey = request.headers.get("X-API-Key")
|
||||
if (apiKey) {
|
||||
return handleApiKeyAuth(apiKey, pathname)
|
||||
}
|
||||
|
||||
if (pathname === '/api/config' && request.method === 'GET') {
|
||||
const session = await auth()
|
||||
if (!session?.user) {
|
||||
return NextResponse.json(
|
||||
{ error: "未授权" },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
if (pathname === '/api/config' && request.method === 'GET') {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
for (const [route, permission] of Object.entries(API_PERMISSIONS)) {
|
||||
if (pathname.startsWith(route)) {
|
||||
const hasAccess = await checkPermission(permission)
|
||||
|
||||
if (!hasAccess) {
|
||||
return NextResponse.json(
|
||||
{ error: "权限不足" },
|
||||
{ status: 403 }
|
||||
)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
for (const [route, permission] of Object.entries(API_PERMISSIONS)) {
|
||||
if (pathname.startsWith(route)) {
|
||||
const hasAccess = await checkPermission(permission)
|
||||
|
||||
if (!hasAccess) {
|
||||
return NextResponse.json(
|
||||
{ error: "权限不足" },
|
||||
{ status: 403 }
|
||||
)
|
||||
}
|
||||
break
|
||||
}
|
||||
// Pages: 语言前缀
|
||||
const segments = pathname.split('/')
|
||||
const maybeLocale = segments[1]
|
||||
const hasLocalePrefix = i18n.locales.includes(maybeLocale as any)
|
||||
if (!hasLocalePrefix) {
|
||||
const cookieLocale = request.headers.get('Cookie')?.match(/NEXT_LOCALE=([^;]+)/)?.[1]
|
||||
const targetLocale = (cookieLocale && i18n.locales.includes(cookieLocale as any)) ? cookieLocale : i18n.defaultLocale
|
||||
const redirectURL = new URL(`/${targetLocale}${pathname}${url.search}`, request.url)
|
||||
return NextResponse.redirect(redirectURL)
|
||||
}
|
||||
|
||||
return NextResponse.next()
|
||||
@@ -55,6 +73,7 @@ export async function middleware(request: Request) {
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
'/((?!_next|.*\\..*).*)', // all pages excluding static assets
|
||||
'/api/emails/:path*',
|
||||
'/api/webhook/:path*',
|
||||
'/api/roles/:path*',
|
||||
|
||||
Reference in New Issue
Block a user