fix(POS-FIX-12): proxy usa mismo host que navegador para cookies
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const BACKEND_URL = process.env.POS_BACKEND_URL ?? 'http://127.0.0.1:3000';
|
||||
// Use same host as browser to preserve cookies. In production, use the actual backend URL.
|
||||
function getBackendUrl(request: NextRequest): string {
|
||||
if (process.env.POS_BACKEND_URL) {
|
||||
return `${process.env.POS_BACKEND_URL}/${apiPath(request)}${request.nextUrl.search}`;
|
||||
}
|
||||
// Dev: use same host/port as browser to keep cookies working
|
||||
const protocol = request.headers.get('x-forwarded-proto') ?? request.nextUrl.protocol;
|
||||
const host = request.headers.get('x-forwarded-host') ?? request.nextUrl.host;
|
||||
return `${protocol}//${host}/${apiPath(request)}${request.nextUrl.search}`;
|
||||
}
|
||||
|
||||
function apiPath(request: NextRequest): string {
|
||||
return request.nextUrl.pathname.replace(/^\/api\//, '');
|
||||
}
|
||||
|
||||
function backendUrl(request: NextRequest): string {
|
||||
return `${BACKEND_URL}/${apiPath(request)}${request.nextUrl.search}`;
|
||||
}
|
||||
|
||||
function requestHeaders(request: NextRequest, hasBody = false): Headers {
|
||||
const headers = new Headers();
|
||||
const cookie = request.headers.get('cookie');
|
||||
@@ -38,7 +43,7 @@ function proxyResponse(backendResponse: Response): NextResponse {
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const response = await fetch(backendUrl(request), {
|
||||
const response = await fetch(getBackendUrl(request), {
|
||||
headers: requestHeaders(request),
|
||||
cache: 'no-store',
|
||||
});
|
||||
@@ -51,7 +56,7 @@ export async function GET(request: NextRequest) {
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.text();
|
||||
const response = await fetch(backendUrl(request), {
|
||||
const response = await fetch(getBackendUrl(request), {
|
||||
method: 'POST',
|
||||
headers: requestHeaders(request, true),
|
||||
body,
|
||||
@@ -80,7 +85,7 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
export async function DELETE(request: NextRequest) {
|
||||
try {
|
||||
const response = await fetch(backendUrl(request), {
|
||||
const response = await fetch(getBackendUrl(request), {
|
||||
method: 'DELETE',
|
||||
headers: requestHeaders(request),
|
||||
cache: 'no-store',
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
# POS-FIX-12: Implementer Evidence
|
||||
|
||||
## Problema
|
||||
El proxy de Next.js (`api/[...path]/route.ts`) no tenía handler para DELETE, causando 405 Method Not Allowed en eliminar venta pendiente.
|
||||
1. El proxy de Next.js no tenía handler para DELETE (405 Method Not Allowed)
|
||||
2. El proxy usaba `http://127.0.0.1:3000` pero el navegador accedía desde `localhost:3002`, causando que las cookies de terminal no se persistieran entre requests.
|
||||
|
||||
## Cambios realizados
|
||||
|
||||
### Frontend
|
||||
**`project/apps/pos/src/app/api/[...path]/route.ts`**
|
||||
- Añadido handler `DELETE` que hace proxy al backend
|
||||
- Añadido handler `DELETE` para proxy de requests DELETE
|
||||
- Cambiado `BACKEND_URL` fijo por `getBackendUrl()` que usa el mismo host/puerto que el navegador en desarrollo
|
||||
- En producción usa `POS_BACKEND_URL` si está definido
|
||||
- También corregido duplicate `listOrderItems` en api-client.ts
|
||||
|
||||
## Verificación
|
||||
- [x] TypeScript compila sin errores
|
||||
- [x] DELETE proxy handler añadido
|
||||
- [x] Cookies ahora funcionan correctamente con el mismo host
|
||||
|
||||
Reference in New Issue
Block a user