modified proxy to use jwt_secret
This commit is contained in:
Generated
+10
@@ -11,6 +11,7 @@
|
|||||||
"axios": "^1.11.0",
|
"axios": "^1.11.0",
|
||||||
"exceljs": "^4.4.0",
|
"exceljs": "^4.4.0",
|
||||||
"file-saver": "^2.0.5",
|
"file-saver": "^2.0.5",
|
||||||
|
"jose": "^6.1.3",
|
||||||
"js-cookie": "^3.0.5",
|
"js-cookie": "^3.0.5",
|
||||||
"next": "^16.0.7",
|
"next": "^16.0.7",
|
||||||
"react": "19.1.0",
|
"react": "19.1.0",
|
||||||
@@ -1874,6 +1875,15 @@
|
|||||||
"integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==",
|
"integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/jose": {
|
||||||
|
"version": "6.1.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/jose/-/jose-6.1.3.tgz",
|
||||||
|
"integrity": "sha512-0TpaTfihd4QMNwrz/ob2Bp7X04yuxJkjRGi4aKmOqwhov54i6u79oCv7T+C7lo70MKH6BesI3vscD1yb/yzKXQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/panva"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/js-cookie": {
|
"node_modules/js-cookie": {
|
||||||
"version": "3.0.5",
|
"version": "3.0.5",
|
||||||
"resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz",
|
"resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz",
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
"axios": "^1.11.0",
|
"axios": "^1.11.0",
|
||||||
"exceljs": "^4.4.0",
|
"exceljs": "^4.4.0",
|
||||||
"file-saver": "^2.0.5",
|
"file-saver": "^2.0.5",
|
||||||
|
"jose": "^6.1.3",
|
||||||
"js-cookie": "^3.0.5",
|
"js-cookie": "^3.0.5",
|
||||||
"next": "^16.0.7",
|
"next": "^16.0.7",
|
||||||
"react": "19.1.0",
|
"react": "19.1.0",
|
||||||
|
|||||||
@@ -1,25 +1,64 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import type { NextRequest } from "next/server";
|
import type { NextRequest } from "next/server";
|
||||||
|
import { jwtVerify } from "jose";
|
||||||
|
|
||||||
export function proxy(request: NextRequest) {
|
if (!process.env.JWT_SECRET) {
|
||||||
|
console.error("coloca el JWT_SECRET en el .env")
|
||||||
|
}
|
||||||
|
|
||||||
|
const secret = new TextEncoder().encode(process.env.JWT_SECRET);
|
||||||
|
|
||||||
|
export async function proxy(request: NextRequest) {
|
||||||
const token = request.cookies.get("token")?.value;
|
const token = request.cookies.get("token")?.value;
|
||||||
const pathname = request.nextUrl.pathname;
|
const pathname = request.nextUrl.pathname;
|
||||||
|
|
||||||
|
if (!token && pathname != "/") {
|
||||||
|
return NextResponse.redirect(new URL("/", request.url));
|
||||||
|
}
|
||||||
|
|
||||||
if (pathname === "/" && token) {
|
if (pathname === "/" && token) {
|
||||||
return NextResponse.redirect(new URL("/Impresiones", request.url));
|
return NextResponse.redirect(new URL("/Impresiones", request.url));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!token && pathname != "/") {
|
if (token) {
|
||||||
console.log("No hay token, redirigiendo a login");
|
try {
|
||||||
return NextResponse.redirect(new URL("/", request.url));
|
const { payload } = await jwtVerify(token, secret);
|
||||||
|
|
||||||
|
const role = payload.role as number;
|
||||||
|
|
||||||
|
const onlyImpresiones = ["/Impresiones"];
|
||||||
|
|
||||||
|
const limitedRoutes = [
|
||||||
|
"/Inscripcion",
|
||||||
|
"/Alta",
|
||||||
|
"/AgregarTiempo",
|
||||||
|
"/Impresiones",
|
||||||
|
"/AsignacionMesas",
|
||||||
|
"/AsignacionEquipo",
|
||||||
|
"/Monitor",
|
||||||
|
];
|
||||||
|
|
||||||
|
if (role === 5 && !onlyImpresiones.includes(pathname)) {
|
||||||
|
return NextResponse.redirect(new URL("/Impresiones", request.url));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((role === 2 || role === 3) && !limitedRoutes.includes(pathname)) {
|
||||||
|
return NextResponse.redirect(new URL("/Impresiones", request.url));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
if (pathname != "/") {
|
||||||
|
return NextResponse.redirect(new URL("/", request.url));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.next();
|
return NextResponse.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
matcher: [
|
matcher: [
|
||||||
"/",
|
"/",
|
||||||
"/Reportes",
|
"/Reportes",
|
||||||
"/Monitor",
|
"/Monitor",
|
||||||
"/QuitarSancion",
|
"/QuitarSancion",
|
||||||
@@ -37,5 +76,4 @@ export const config = {
|
|||||||
"/AgregarTiempo",
|
"/AgregarTiempo",
|
||||||
"/ActivosMantenimiento",
|
"/ActivosMantenimiento",
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
//IO
|
|
||||||
Reference in New Issue
Block a user