Files

31 lines
914 B
TypeScript

import { NextResponse } from "next/server";
import { cookies } from "next/headers";
import type { AuthUser } from "@duoc-thu/shared-types";
import { SESSION_COOKIE } from "../session";
const GATEWAY_URL = process.env.API_GATEWAY_URL ?? "http://localhost:3000";
export async function GET() {
const token = cookies().get(SESSION_COOKIE)?.value;
if (!token) {
return NextResponse.json({ error: "not_authenticated" }, { status: 401 });
}
let upstream: Response;
try {
upstream = await fetch(`${GATEWAY_URL}/auth/me`, {
headers: { Authorization: `Bearer ${token}` },
cache: "no-store",
});
} catch {
return NextResponse.json({ error: "gateway_unreachable" }, { status: 502 });
}
if (!upstream.ok) {
return NextResponse.json({ error: "not_authenticated" }, { status: 401 });
}
const user = (await upstream.json()) as AuthUser;
return NextResponse.json(user);
}