-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
30 lines (23 loc) · 928 Bytes
/
middleware.ts
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
import { type NextRequest, NextResponse } from "next/server";
export function middleware(request: NextRequest) {
const token = request.cookies.get("sb-access-token")?.value; // Verifica o cookie de token
const pathname = request.nextUrl.pathname;
if (token && pathname === "/login") {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
if (token && pathname === "/register") {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
if (token && pathname === "/") {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
// Se tentar acessar uma rota protegida (dashboard) sem estar logado
if (pathname.startsWith("/dashboard") && !token) {
return NextResponse.redirect(new URL("/login", request.url));
}
// Continua normalmente
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|public).*)"],
};