-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
64 lines (55 loc) · 1.75 KB
/
Copy pathproxy.ts
File metadata and controls
64 lines (55 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// proxy.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";
// Renomeado para 'proxy' conforme convenção do Next 16
export async function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
// ROTAS PÚBLICAS EXATAS (só permite se for estritamente igual)
const exactPublicPaths = ["/"];
// ROTAS PÚBLICAS POR PREFIXO (permite se começar com esses caminhos)
const prefixPublicPaths = [
"/totem/idle",
"/totem/check-in",
"/totem/success",
"/totem/error",
"/admin/login",
"/admin/register",
"/forgot-password",
"/check-email",
"/verify-email",
"/api/auth",
"/api/totem",
"/api/settings/public",
];
// Verifica se a rota bate com a exata OU com algum dos prefixos
const isPublicPath =
exactPublicPaths.includes(pathname) ||
prefixPublicPaths.some((path) => pathname.startsWith(path));
if (isPublicPath) {
return NextResponse.next();
}
// 🔒 ROTAS PROTEGIDAS: Verifica se tem token de autenticação
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET,
});
// Se não tem token e está tentando acessar rota protegida
if (!token && pathname.startsWith("/admin")) {
const url = new URL("/admin/login", request.url);
url.searchParams.set("callbackUrl", pathname);
return NextResponse.redirect(url);
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!_next/static|_next/image|favicon.ico).*)",
],
};