33 lines
1.8 KiB
TypeScript
33 lines
1.8 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/db";
|
|
import { actorLabel, requireUser } from "@/lib/auth/user";
|
|
import { audit } from "@/lib/audit";
|
|
|
|
/** Facebook logins that can supply pages. Tokens never leave the server. */
|
|
export async function GET() {
|
|
const auth = await requireUser();
|
|
if (!auth.ok) return auth.response;
|
|
const connections = await prisma.metaConnection.findMany({
|
|
where: auth.user.globalRole === "SUPERADMIN" ? undefined : { connectedById: auth.user.id },
|
|
select: { id: true, name: true, scopes: true, expiresAt: true, updatedAt: true, _count: { select: { pages: true } } },
|
|
orderBy: { createdAt: "asc" },
|
|
});
|
|
return NextResponse.json({ connections, systemUser: auth.user.globalRole === "SUPERADMIN" && Boolean(process.env.META_SYSTEM_USER_TOKEN) });
|
|
}
|
|
|
|
/**
|
|
* Forget a Facebook login. Pages already connected keep their own page tokens
|
|
* and keep working; only re-listing pages from this login stops.
|
|
*/
|
|
export async function DELETE(req: Request) {
|
|
const auth = await requireUser();
|
|
if (!auth.ok) return auth.response;
|
|
const id = new URL(req.url).searchParams.get("id") ?? "";
|
|
const conn = await prisma.metaConnection.findUnique({ where: { id }, select: { name: true, connectedById: true } });
|
|
if (!conn) return NextResponse.json({ error: "ไม่พบการเชื่อมนี้" }, { status: 404 });
|
|
if (auth.user.globalRole !== "SUPERADMIN" && conn.connectedById !== auth.user.id) return NextResponse.json({ error: "ไม่พบการเชื่อมนี้" }, { status: 404 });
|
|
await prisma.metaConnection.delete({ where: { id } });
|
|
await audit({ action: "META_LOGIN_REMOVED", actorId: auth.user.id, actorLabel: actorLabel(auth.user), detail: { name: conn.name } });
|
|
return NextResponse.json({ ok: true });
|
|
}
|