hopefully final username and source/lens submission fix
This commit is contained in:
parent
28d363cdfe
commit
23ede9c872
5 changed files with 736 additions and 195 deletions
|
|
@ -1,28 +1,34 @@
|
||||||
// app/api/me/route.ts
|
// app/api/me/route.ts
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
/** Read a cookie value from a raw Cookie header string */
|
||||||
function readCookie(name: string, cookieHeader: string) {
|
function readCookie(name: string, cookieHeader: string) {
|
||||||
const m = cookieHeader.match(new RegExp(`(?:^|;\\s*)${name}=([^;]+)`));
|
const m = cookieHeader.match(new RegExp(`(?:^|;\\s*)${name}=([^;]+)`));
|
||||||
return m?.[1] ?? null;
|
return m?.[1] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(req: Request) {
|
export async function GET(req: Request) {
|
||||||
const base = process.env.NEXT_PUBLIC_API_BASE_URL!;
|
// Prefer DIRECTUS_URL if present; fall back to NEXT_PUBLIC_API_BASE_URL
|
||||||
const url = `${base}/users/me?fields=id,username,display_name,first_name,last_name,email`;
|
const base =
|
||||||
|
process.env.DIRECTUS_URL || process.env.NEXT_PUBLIC_API_BASE_URL || "";
|
||||||
|
const url = `${base.replace(/\/$/, "")}/users/me?fields=id,username,display_name,first_name,last_name,email`;
|
||||||
|
|
||||||
|
// Forward the incoming cookies (session), and also ma_at as Bearer (token setups)
|
||||||
const cookieHeader = req.headers.get("cookie") ?? "";
|
const cookieHeader = req.headers.get("cookie") ?? "";
|
||||||
const ma_at = readCookie("ma_at", cookieHeader);
|
const ma_at = readCookie("ma_at", cookieHeader);
|
||||||
|
|
||||||
const headers: Record<string, string> = { "cache-control": "no-store" };
|
const headers: Record<string, string> = { "cache-control": "no-store" };
|
||||||
if (cookieHeader) headers.cookie = cookieHeader; // session cookie
|
if (cookieHeader) headers.cookie = cookieHeader;
|
||||||
if (ma_at) headers.authorization = `Bearer ${ma_at}`; // direct token (if present)
|
if (ma_at) headers.authorization = `Bearer ${ma_at}`;
|
||||||
|
|
||||||
const res = await fetch(url, { headers, cache: "no-store" });
|
const res = await fetch(url, { headers, cache: "no-store" });
|
||||||
const raw = await res.json().catch(() => ({}));
|
const body = await res.json().catch(() => ({}));
|
||||||
const data = raw?.data ?? raw ?? null; // normalize
|
|
||||||
|
|
||||||
return NextResponse.json(
|
return new NextResponse(JSON.stringify(body), {
|
||||||
{ data },
|
status: res.status,
|
||||||
{ status: res.status, headers: { "content-type": "application/json", "cache-control": "no-store" } }
|
headers: {
|
||||||
);
|
"content-type": "application/json",
|
||||||
|
"cache-control": "no-store",
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,41 +3,39 @@ export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
const BASE =
|
const BASE = (
|
||||||
(process.env.DIRECTUS_URL || process.env.NEXT_PUBLIC_API_BASE_URL || "").replace(/\/$/, "");
|
process.env.DIRECTUS_URL || process.env.NEXT_PUBLIC_API_BASE_URL || ""
|
||||||
|
).replace(/\/$/, "");
|
||||||
|
|
||||||
function buildPath(target?: string | null) {
|
function buildPath() {
|
||||||
|
// This collection uses make/model (no `name` field)
|
||||||
const url = new URL(`${BASE}/items/laser_source`);
|
const url = new URL(`${BASE}/items/laser_source`);
|
||||||
url.searchParams.set("fields", "id,name");
|
url.searchParams.set("fields", "id,make,model");
|
||||||
url.searchParams.set("sort", "name");
|
url.searchParams.set("sort", "make,model");
|
||||||
// if (target) url.searchParams.set("filter[target][_eq]", target);
|
|
||||||
return String(url);
|
return String(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
function readCookieFromHeader(name: string, cookieHeader: string) {
|
async function dFetch(bearer: string) {
|
||||||
const m = cookieHeader.match(new RegExp(`(?:^|;\\s*)${name}=([^;]+)`));
|
const res = await fetch(buildPath(), {
|
||||||
return m?.[1] ?? null;
|
headers: { Accept: "application/json", Authorization: `Bearer ${bearer}` },
|
||||||
}
|
|
||||||
|
|
||||||
async function dFetch(token: string, target?: string | null) {
|
|
||||||
const res = await fetch(buildPath(target), {
|
|
||||||
headers: { Accept: "application/json", Authorization: `Bearer ${token}` },
|
|
||||||
cache: "no-store",
|
cache: "no-store",
|
||||||
});
|
});
|
||||||
const text = await res.text().catch(() => "");
|
const text = await res.text().catch(() => "");
|
||||||
let json: any = null;
|
let json: any = null;
|
||||||
try { json = text ? JSON.parse(text) : null; } catch {}
|
try {
|
||||||
|
json = text ? JSON.parse(text) : null;
|
||||||
|
} catch {}
|
||||||
return { res, json, text };
|
return { res, json, text };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(req: NextRequest) {
|
export async function GET(req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const cookieHeader = req.headers.get("cookie") ?? "";
|
const userAt = req.cookies.get("ma_at")?.value;
|
||||||
const userAt = req.cookies.get("ma_at")?.value || readCookieFromHeader("ma_at", cookieHeader);
|
if (!userAt) {
|
||||||
if (!userAt) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
const target = req.nextUrl.searchParams.get("target");
|
const r = await dFetch(userAt);
|
||||||
const r = await dFetch(userAt, target);
|
|
||||||
if (!r.res.ok) {
|
if (!r.res.ok) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: `Directus ${r.res.status}: ${r.text || r.res.statusText}` },
|
{ error: `Directus ${r.res.status}: ${r.text || r.res.statusText}` },
|
||||||
|
|
@ -45,14 +43,20 @@ export async function GET(req: NextRequest) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const rows: Array<{ id: number | string; name?: string; label?: string; title?: string }> =
|
const rows: Array<{ id: string | number; make?: string; model?: string }> =
|
||||||
r.json?.data ?? [];
|
r.json?.data ?? [];
|
||||||
const data = rows.map(({ id, name, label, title }) => ({
|
|
||||||
id,
|
// Compose human label from make + model
|
||||||
name: name || label || title || "",
|
const data = rows.map(({ id, make, model }) => {
|
||||||
}));
|
const label = [make, model].filter(Boolean).join(" ");
|
||||||
|
return { id, name: label, label };
|
||||||
|
});
|
||||||
|
|
||||||
return NextResponse.json({ data });
|
return NextResponse.json({ data });
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return NextResponse.json({ error: e?.message || "Failed to load laser sources" }, { status: 500 });
|
return NextResponse.json(
|
||||||
|
{ error: e?.message || "Failed to load laser sources" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,38 +3,47 @@ export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
const BASE =
|
const BASE = (
|
||||||
(process.env.DIRECTUS_URL || process.env.NEXT_PUBLIC_API_BASE_URL || "").replace(/\/$/, "");
|
process.env.DIRECTUS_URL || process.env.NEXT_PUBLIC_API_BASE_URL || ""
|
||||||
|
).replace(/\/$/, "");
|
||||||
|
|
||||||
function buildPath(target?: string | null) {
|
function buildPath(target?: string | null) {
|
||||||
|
// CO2 Gantry → focus lenses (name)
|
||||||
|
// everything else (Fiber/UV/CO2 Galvo) → scan lenses (field_size + focal_length)
|
||||||
|
const isGantry = target === "co2-gantry";
|
||||||
|
|
||||||
|
if (isGantry) {
|
||||||
|
const url = new URL(`${BASE}/items/laser_focus_lens`);
|
||||||
|
url.searchParams.set("fields", "id,name");
|
||||||
|
url.searchParams.set("sort", "name");
|
||||||
|
return String(url);
|
||||||
|
}
|
||||||
|
|
||||||
const url = new URL(`${BASE}/items/laser_scan_lens`);
|
const url = new URL(`${BASE}/items/laser_scan_lens`);
|
||||||
url.searchParams.set("fields", "id,name");
|
url.searchParams.set("fields", "id,field_size,focal_length");
|
||||||
url.searchParams.set("sort", "name");
|
url.searchParams.set("sort", "field_size,focal_length");
|
||||||
// if (target) url.searchParams.set("filter[target][_eq]", target);
|
|
||||||
return String(url);
|
return String(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
function readCookieFromHeader(name: string, cookieHeader: string) {
|
async function dFetch(bearer: string, target?: string | null) {
|
||||||
const m = cookieHeader.match(new RegExp(`(?:^|;\\s*)${name}=([^;]+)`));
|
|
||||||
return m?.[1] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dFetch(token: string, target?: string | null) {
|
|
||||||
const res = await fetch(buildPath(target), {
|
const res = await fetch(buildPath(target), {
|
||||||
headers: { Accept: "application/json", Authorization: `Bearer ${token}` },
|
headers: { Accept: "application/json", Authorization: `Bearer ${bearer}` },
|
||||||
cache: "no-store",
|
cache: "no-store",
|
||||||
});
|
});
|
||||||
const text = await res.text().catch(() => "");
|
const text = await res.text().catch(() => "");
|
||||||
let json: any = null;
|
let json: any = null;
|
||||||
try { json = text ? JSON.parse(text) : null; } catch {}
|
try {
|
||||||
|
json = text ? JSON.parse(text) : null;
|
||||||
|
} catch {}
|
||||||
return { res, json, text };
|
return { res, json, text };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(req: NextRequest) {
|
export async function GET(req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const cookieHeader = req.headers.get("cookie") ?? "";
|
const userAt = req.cookies.get("ma_at")?.value;
|
||||||
const userAt = req.cookies.get("ma_at")?.value || readCookieFromHeader("ma_at", cookieHeader);
|
if (!userAt) {
|
||||||
if (!userAt) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
const target = req.nextUrl.searchParams.get("target");
|
const target = req.nextUrl.searchParams.get("target");
|
||||||
const r = await dFetch(userAt, target);
|
const r = await dFetch(userAt, target);
|
||||||
|
|
@ -45,14 +54,32 @@ export async function GET(req: NextRequest) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const rows: Array<{ id: number | string; name?: string; label?: string; title?: string }> =
|
const rows: any[] = r.json?.data ?? [];
|
||||||
r.json?.data ?? [];
|
const isGantry = target === "co2-gantry";
|
||||||
const data = rows.map(({ id, name, label, title }) => ({
|
|
||||||
id,
|
const data = rows.map((row) => {
|
||||||
name: name || label || title || "",
|
if (isGantry) {
|
||||||
}));
|
// Focus lens: label is just the stored name
|
||||||
|
return { id: row.id, name: row.name, label: row.name };
|
||||||
|
}
|
||||||
|
// Scan lens: label "300x300 mm | F420" etc
|
||||||
|
const fs =
|
||||||
|
row.field_size != null && row.field_size !== ""
|
||||||
|
? `${row.field_size} mm`
|
||||||
|
: "";
|
||||||
|
const fl =
|
||||||
|
row.focal_length != null && row.focal_length !== ""
|
||||||
|
? `F${row.focal_length}`
|
||||||
|
: "";
|
||||||
|
const label = [fs, fl].filter(Boolean).join(" | ");
|
||||||
|
return { id: row.id, name: label, label };
|
||||||
|
});
|
||||||
|
|
||||||
return NextResponse.json({ data });
|
return NextResponse.json({ data });
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return NextResponse.json({ error: e?.message || "Failed to load lenses" }, { status: 500 });
|
return NextResponse.json(
|
||||||
|
{ error: e?.message || "Failed to load lenses" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
BIN
makearmy-app.zip
Normal file
BIN
makearmy-app.zip
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue