middleware: aplicar em todas as páginas (edge)\n\n- Remove runtime nodejs (middleware roda em Edge por padrão)\n- Ajusta matcher para ignorar assets e imagens estáticas\n- Mantém /login como público e gateia o restante por sessão\n- Admin-only aplicado em produção.\n\nEvita casos em que páginas eram renderizadas sem dados em anônimo ao acessar rotas diretas.
This commit is contained in:
parent
7e480fe864
commit
0177391326
1 changed files with 8 additions and 6 deletions
|
|
@ -1,16 +1,16 @@
|
||||||
import { NextRequest, NextResponse } from "next/server"
|
import { NextRequest, NextResponse } from "next/server"
|
||||||
import { getCookieCache } from "better-auth/cookies"
|
import { getCookieCache } from "better-auth/cookies"
|
||||||
|
|
||||||
const PUBLIC_PATHS = [/^\/login$/, /^\/api\/auth/, /^\/_next\//, /^\/favicon/]
|
// Rotas públicas explícitas (não autenticadas)
|
||||||
|
const PUBLIC_PATHS = [/^\/login$/]
|
||||||
|
// Rotas somente admin
|
||||||
const ADMIN_ONLY_PATHS = [/^\/admin(?:$|\/)/]
|
const ADMIN_ONLY_PATHS = [/^\/admin(?:$|\/)/]
|
||||||
const APP_HOME = "/dashboard"
|
const APP_HOME = "/dashboard"
|
||||||
|
|
||||||
export async function middleware(request: NextRequest) {
|
export async function middleware(request: NextRequest) {
|
||||||
const { pathname, search } = request.nextUrl
|
const { pathname, search } = request.nextUrl
|
||||||
|
|
||||||
if (PUBLIC_PATHS.some((pattern) => pattern.test(pathname))) {
|
if (PUBLIC_PATHS.some((pattern) => pattern.test(pathname))) return NextResponse.next()
|
||||||
return NextResponse.next()
|
|
||||||
}
|
|
||||||
|
|
||||||
const session = await getCookieCache(request)
|
const session = await getCookieCache(request)
|
||||||
|
|
||||||
|
|
@ -37,6 +37,8 @@ export async function middleware(request: NextRequest) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
runtime: "nodejs",
|
// Evita executar para assets e imagens estáticas
|
||||||
matcher: ["/(.*)"],
|
matcher: [
|
||||||
|
"/((?!api|_next/static|_next/image|favicon.ico|icon.png).*)",
|
||||||
|
],
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue