Files
taller_automotriz/src/middleware.ts
T
Beldor 62dab5e025
Revisión con IA / ai-review (pull_request) Successful in 2m18s
feat(cliente): ver y modificar los datos de contacto
El cliente no tenia forma de corregir su email ni su telefono despues del alta.
Nueva pantalla /cliente/perfil ("Mis datos", enlazada desde el dashboard) que
muestra nombre, email y telefono, y permite editarlos.

- GET/PATCH /api/cliente/perfil, ambos con la sesion del cliente.
- El telefono usa el mismo campo con el +54 9 fijo que el registro, y se guarda
  normalizado en telefonoNorm para que el bot lo siga reconociendo.
- aLocalArgentino() saca el prefijo para mostrar: un cliente dado de alta por
  WhatsApp tiene el numero guardado como 5491141984844 y en el formulario tiene
  que verse 1141984844.
- Email y telefono se validan unicos contra los demas clientes (409), no contra
  uno mismo. Si cambia el email se avisa que con ese va a entrar de ahora en mas,
  porque es la identidad del magic link.
- El middleware protege /cliente/perfil como el resto del area del cliente.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-05 15:54:38 -03:00

49 lines
1.5 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { jwtVerify } from "jose";
const SECRET = new TextEncoder().encode(process.env.NEXTAUTH_SECRET || "dev-secret");
async function verifyCookie(req: NextRequest, cookieName: string) {
const token = req.cookies.get(cookieName)?.value;
if (!token) return false;
try {
await jwtVerify(token, SECRET);
return true;
} catch {
return false;
}
}
export async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
if (pathname.startsWith("/cliente/dashboard") ||
pathname.startsWith("/cliente/historial") ||
pathname.startsWith("/cliente/perfil") ||
pathname.startsWith("/cliente/turnos")) {
const ok = await verifyCookie(req, "cliente_session");
if (!ok) return NextResponse.redirect(new URL("/cliente", req.url));
}
if (pathname.startsWith("/admin/dashboard") ||
pathname.startsWith("/admin/turnos") ||
pathname.startsWith("/admin/historial") ||
pathname.startsWith("/admin/servicios") ||
pathname.startsWith("/admin/jefes") ||
pathname.startsWith("/admin/clientes")) {
const ok = await verifyCookie(req, "admin_session");
if (!ok) return NextResponse.redirect(new URL("/admin", req.url));
}
if (pathname.startsWith("/jefe/dashboard")) {
const ok = await verifyCookie(req, "jefe_session");
if (!ok) return NextResponse.redirect(new URL("/jefe", req.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/cliente/:path*", "/admin/:path*", "/jefe/:path*"],
};