feat(ADM-018): completed feature

This commit is contained in:
chattie
2026-08-17 22:23:10 +02:00
parent cf1c69fc8b
commit d595b4871f
871 changed files with 47411 additions and 281 deletions

View File

@@ -0,0 +1,46 @@
import { NextRequest, NextResponse } from 'next/server';
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
/**
* Strip the Secure flag from the backend's Set-Cookie so the browser
* (which connects over HTTP) actually stores the session cookie.
* Also drop SameSite=Lax to avoid browser restrictions.
*/
function makeLocalhostCompatible(cookie: string): string {
return cookie
.replace(/;\s*Secure/gi, '')
.replace(/;\s*SameSite=Lax/gi, '')
.trim();
}
export async function POST(req: NextRequest) {
try {
const body = await req.json();
const { email, password } = body;
const backendRes = await fetch(`${API}/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password }),
});
const data = await backendRes.json();
if (!backendRes.ok) {
return NextResponse.json(data, { status: backendRes.status });
}
const setCookie = backendRes.headers.get('set-cookie');
const response = NextResponse.json(data, { status: 200 });
if (setCookie) {
response.headers.set('Set-Cookie', makeLocalhostCompatible(setCookie));
}
return response;
} catch {
return NextResponse.json(
{ statusCode: 500, code: 'SERVER_ERROR', message: 'Error del servidor' },
{ status: 500 },
);
}
}

View File

@@ -0,0 +1,19 @@
import { NextRequest, NextResponse } from 'next/server';
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
export async function POST(req: NextRequest) {
try {
const cookies = req.headers.get('cookie') ?? '';
await fetch(`${API}/auth/logout`, {
method: 'POST',
headers: { Cookie: cookies },
});
} catch {
// Best-effort
}
const response = NextResponse.json({ ok: true });
response.cookies.delete('mdv_session');
return response;
}

View File

@@ -0,0 +1,16 @@
import { NextRequest, NextResponse } from 'next/server';
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
export async function GET(req: NextRequest) {
const cookies = req.headers.get('cookie') ?? '';
try {
const backendRes = await fetch(`${API}/auth/me`, {
headers: { Cookie: cookies },
});
if (!backendRes.ok) return NextResponse.json({ user: null });
return NextResponse.json(await backendRes.json());
} catch {
return NextResponse.json({ user: null });
}
}