Files
co-lite/solvway-admin-portal/app/api/login-security/[[...path]]/route.ts
T

31 lines
1.6 KiB
TypeScript

import { NextResponse, type NextRequest } from "next/server";
import { callMobileApi } from "@/app/lib/server/mobileApi";
import { getToken } from "@/app/lib/server/session";
import { badRequest, errorResponse, unauthorized } from "@/app/lib/server/apiRoute";
// Proxy for /api/admin/login-security/**; mobile-api owns the BO_LOGIN_SECURITY guard. Bodies are forwarded, never logged.
type Ctx = { params: Promise<{ path?: string[] }> };
async function forward(method: "GET" | "POST" | "PUT", req: NextRequest, { params }: Ctx) {
const token = await getToken();
if (!token) return unauthorized();
const { path = [] } = await params;
if (path.some((s) => s === "." || s === "..")) return badRequest("Invalid path.");
const target = `/api/admin/login-security${path.map(encodeURIComponent).map((s) => `/${s}`).join("")}${method === "GET" ? new URL(req.url).search : ""}`;
let body: unknown;
if (method !== "GET") {
const text = await req.text();
if (text) {
try { body = JSON.parse(text); } catch { return badRequest("Invalid request body."); }
}
}
const result = await callMobileApi<unknown>(target, method === "GET" ? { token } : { token, method, body });
if (!result.ok) return errorResponse(result);
if (result.data === null || result.data === undefined) return new NextResponse(null, { status: 204 });
return NextResponse.json(result.data);
}
export const GET = (req: NextRequest, ctx: Ctx) => forward("GET", req, ctx);
export const POST = (req: NextRequest, ctx: Ctx) => forward("POST", req, ctx);
export const PUT = (req: NextRequest, ctx: Ctx) => forward("PUT", req, ctx);