Files
voyage/apps/public-web/app/(site)/admin/page.tsx

38 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { redirect } from "next/navigation";
import { cookies } from "next/headers";
export default async function AdminPage() {
const cookieHeader = (await cookies())
.getAll()
.map(({ name, value }) => `${name}=${value}`)
.join("; ");
const res = await fetch(`${process.env.BACKEND_URL}/api/me`, {
headers: { cookie: cookieHeader },
cache: "no-store",
redirect: "manual", // IMPORTANT: dont follow to /login
});
// Spring might answer 302 to /login when unauthenticated
if (res.status === 401 || res.status === 302) redirect("/login");
if (!res.ok) {
const text = await res.text();
throw new Error(`Failed to load session: ${res.status} ${text.slice(0, 200)}`);
}
const contentType = res.headers.get("content-type") ?? "";
if (!contentType.includes("application/json")) {
// we got HTML (login page) or something else
redirect("/login");
}
const me = await res.json();
return (
<main>
<h1>Admin</h1>
<pre>{JSON.stringify(me, null, 2)}</pre>
</main>
);
}