basic login implementation at localhost:3000//login
This commit is contained in:
38
apps/public-web/app/(site)/admin/page.tsx
Normal file
38
apps/public-web/app/(site)/admin/page.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
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: don’t 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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user