Spaces:
Sleeping
Sleeping
fix: fix same origin error
Browse files
frontend/app/admin/login/page.tsx
CHANGED
|
@@ -20,6 +20,7 @@ export default function LoginPage() {
|
|
| 20 |
try {
|
| 21 |
const r = await fetch("/api/admin/login", {
|
| 22 |
method: "POST",
|
|
|
|
| 23 |
headers: { "Content-Type": "application/json" },
|
| 24 |
body: JSON.stringify({ passcode }),
|
| 25 |
});
|
|
|
|
| 20 |
try {
|
| 21 |
const r = await fetch("/api/admin/login", {
|
| 22 |
method: "POST",
|
| 23 |
+
credentials: "same-origin",
|
| 24 |
headers: { "Content-Type": "application/json" },
|
| 25 |
body: JSON.stringify({ passcode }),
|
| 26 |
});
|
frontend/app/admin/page.tsx
CHANGED
|
@@ -16,14 +16,14 @@ export default function AdminPage() {
|
|
| 16 |
|
| 17 |
useEffect(() => {
|
| 18 |
(async () => {
|
| 19 |
-
const r = await fetch("/api/admin/me");
|
| 20 |
if (!r.ok) router.replace("/admin/login");
|
| 21 |
else setChecking(false);
|
| 22 |
})();
|
| 23 |
}, [router]);
|
| 24 |
|
| 25 |
async function logout() {
|
| 26 |
-
await fetch("/api/admin/logout", { method: "POST" });
|
| 27 |
router.replace("/admin/login");
|
| 28 |
}
|
| 29 |
|
|
|
|
| 16 |
|
| 17 |
useEffect(() => {
|
| 18 |
(async () => {
|
| 19 |
+
const r = await fetch("/api/admin/me", { credentials: "same-origin" });
|
| 20 |
if (!r.ok) router.replace("/admin/login");
|
| 21 |
else setChecking(false);
|
| 22 |
})();
|
| 23 |
}, [router]);
|
| 24 |
|
| 25 |
async function logout() {
|
| 26 |
+
await fetch("/api/admin/logout", { method: "POST", credentials: "same-origin" });
|
| 27 |
router.replace("/admin/login");
|
| 28 |
}
|
| 29 |
|
frontend/app/api/admin/login/route.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { type NextRequest, NextResponse } from "next/server";
|
| 2 |
+
import { backendUrl } from "@/lib/backend-url";
|
| 3 |
+
|
| 4 |
+
export const dynamic = "force-dynamic";
|
| 5 |
+
export const runtime = "nodejs";
|
| 6 |
+
|
| 7 |
+
export async function POST(request: NextRequest) {
|
| 8 |
+
let response: Response;
|
| 9 |
+
try {
|
| 10 |
+
response = await fetch(`${backendUrl()}/admin/login`, {
|
| 11 |
+
method: "POST",
|
| 12 |
+
body: await request.text(),
|
| 13 |
+
headers: {
|
| 14 |
+
"content-type": request.headers.get("content-type") ?? "application/json",
|
| 15 |
+
},
|
| 16 |
+
});
|
| 17 |
+
} catch {
|
| 18 |
+
return NextResponse.json(
|
| 19 |
+
{ detail: "Could not reach the backend admin login service." },
|
| 20 |
+
{ status: 502 }
|
| 21 |
+
);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
const headers = new Headers();
|
| 25 |
+
headers.set("content-type", response.headers.get("content-type") ?? "application/json");
|
| 26 |
+
const cookie = response.headers.get("set-cookie");
|
| 27 |
+
if (cookie) headers.set("set-cookie", cookie);
|
| 28 |
+
|
| 29 |
+
return new NextResponse(await response.text(), {
|
| 30 |
+
status: response.status,
|
| 31 |
+
headers,
|
| 32 |
+
});
|
| 33 |
+
}
|
| 34 |
+
|
frontend/app/api/admin/logout/route.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { type NextRequest, NextResponse } from "next/server";
|
| 2 |
+
import { backendUrl } from "@/lib/backend-url";
|
| 3 |
+
|
| 4 |
+
export const dynamic = "force-dynamic";
|
| 5 |
+
export const runtime = "nodejs";
|
| 6 |
+
|
| 7 |
+
export async function POST(request: NextRequest) {
|
| 8 |
+
let response: Response;
|
| 9 |
+
try {
|
| 10 |
+
response = await fetch(`${backendUrl()}/admin/logout`, {
|
| 11 |
+
method: "POST",
|
| 12 |
+
headers: {
|
| 13 |
+
cookie: request.headers.get("cookie") ?? "",
|
| 14 |
+
},
|
| 15 |
+
});
|
| 16 |
+
} catch {
|
| 17 |
+
return NextResponse.json(
|
| 18 |
+
{ detail: "Could not reach the backend admin logout service." },
|
| 19 |
+
{ status: 502 }
|
| 20 |
+
);
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
const headers = new Headers();
|
| 24 |
+
headers.set("content-type", response.headers.get("content-type") ?? "application/json");
|
| 25 |
+
const cookie = response.headers.get("set-cookie");
|
| 26 |
+
if (cookie) headers.set("set-cookie", cookie);
|
| 27 |
+
|
| 28 |
+
return new NextResponse(await response.text(), {
|
| 29 |
+
status: response.status,
|
| 30 |
+
headers,
|
| 31 |
+
});
|
| 32 |
+
}
|
| 33 |
+
|
frontend/app/api/admin/me/route.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { type NextRequest, NextResponse } from "next/server";
|
| 2 |
+
import { backendUrl } from "@/lib/backend-url";
|
| 3 |
+
|
| 4 |
+
export const dynamic = "force-dynamic";
|
| 5 |
+
export const runtime = "nodejs";
|
| 6 |
+
|
| 7 |
+
export async function GET(request: NextRequest) {
|
| 8 |
+
let response: Response;
|
| 9 |
+
try {
|
| 10 |
+
response = await fetch(`${backendUrl()}/admin/me`, {
|
| 11 |
+
headers: {
|
| 12 |
+
cookie: request.headers.get("cookie") ?? "",
|
| 13 |
+
},
|
| 14 |
+
cache: "no-store",
|
| 15 |
+
});
|
| 16 |
+
} catch {
|
| 17 |
+
return NextResponse.json(
|
| 18 |
+
{ detail: "Could not reach the backend admin session service." },
|
| 19 |
+
{ status: 502 }
|
| 20 |
+
);
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
return new NextResponse(await response.text(), {
|
| 24 |
+
status: response.status,
|
| 25 |
+
headers: {
|
| 26 |
+
"content-type": response.headers.get("content-type") ?? "application/json",
|
| 27 |
+
"cache-control": "no-store",
|
| 28 |
+
},
|
| 29 |
+
});
|
| 30 |
+
}
|
| 31 |
+
|