makearmy-app/app/api/me/route.ts

32 lines
1.1 KiB
TypeScript
Raw Normal View History

// app/api/me/route.ts
import { NextResponse } from "next/server";
2025-09-28 01:57:06 -04:00
function readCookie(name: string, cookieHeader: string) {
const m = cookieHeader.match(new RegExp(`(?:^|;\\s*)${name}=([^;]+)`));
return m?.[1] ?? null;
}
export async function GET(req: Request) {
const base = process.env.NEXT_PUBLIC_API_BASE_URL!;
2025-09-28 07:23:31 -04:00
// include username so the form can show it
2025-09-28 07:04:42 -04:00
const url = `${base}/users/me?fields=id,username,display_name,first_name,last_name,email`;
2025-09-28 01:57:06 -04:00
const cookieHeader = req.headers.get("cookie") ?? "";
const ma_at = readCookie("ma_at", cookieHeader);
2025-09-28 01:54:33 -04:00
2025-09-28 07:23:31 -04:00
const headers: Record<string, string> = { "cache-control": "no-store" };
2025-09-28 01:54:33 -04:00
if (cookieHeader) headers.cookie = cookieHeader;
if (ma_at) headers.authorization = `Bearer ${ma_at}`;
2025-09-28 07:04:42 -04:00
const res = await fetch(url, { headers, cache: "no-store" });
const body = await res.json().catch(() => ({}));
return new NextResponse(JSON.stringify(body), {
status: res.status,
2025-09-28 01:57:06 -04:00
headers: {
"content-type": "application/json",
"cache-control": "no-store",
},
});
}