-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
54 lines (44 loc) · 1.59 KB
/
proxy.ts
File metadata and controls
54 lines (44 loc) · 1.59 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
import { NextResponse, NextRequest } from "next/server";
export async function proxy(request: NextRequest) {
const protectedRoutes = ['/ui/pages/Dashboard', '/ui/pages/Dashboard/Admin', '/ui/pages/Dashboard/User', '/api/admin', '/api/user', '/'];
const isLoggedIn = request.cookies.get('auth-token');
const pathname = request.nextUrl.pathname;
console.log('🔐 Cookies disponibles:', request.cookies.getAll());
console.log('🔑 Token encontrado:', !!isLoggedIn);
console.log('📍 Ruta actual:', pathname);
const isProtectedRoute = protectedRoutes.some(route =>
pathname.startsWith(route)
);
if (!isProtectedRoute) {
console.log('✅ Ruta pública, acceso permitido');
return NextResponse.next();
}
if (!isLoggedIn) {
console.log('🚫 No hay token, redirigiendo al login');
if (pathname.startsWith('/api/')) {
return NextResponse.json(
{ error: 'No autorizado' },
{ status: 401 }
);
}
const loginUrl = new URL('/ui/pages/Login', request.url);
loginUrl.searchParams.set('from', pathname);
return NextResponse.redirect(loginUrl);
}
if (pathname === '/') {
console.log('🏠 Redirigiendo ruta raíz al dashboard');
return NextResponse.redirect(new URL('/ui/pages/Dashboard', request.url));
}
console.log('✅ Acceso permitido a ruta protegida:', pathname);
return NextResponse.next();
}
export const config = {
matcher: [
'/ui/pages/Dashboard/Admin/:path*',
'/ui/pages/Dashboard/User/:path*',
'/ui/pages/Dashboard/:path*',
'/api/admin/:path*',
'/api/user/:path*',
'/'
]
};