Files
portabase/app/api/auth/[...all]/route.ts
T

38 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-05-16 15:54:17 +02:00
import { auth } from "@/lib/auth/auth";
import { toNextJsHandler } from "better-auth/next-js";
2026-05-28 10:18:28 +02:00
import { NextRequest, NextResponse } from "next/server";
import { headers } from "next/headers";
2025-05-16 15:54:17 +02:00
2026-05-28 10:18:28 +02:00
const authHandler = toNextJsHandler(auth.handler);
2026-06-27 17:08:38 +02:00
async function blockApiKeyCreateForRestrictedUsers(
req: NextRequest,
): Promise<NextResponse | null> {
const url = req.nextUrl;
if (req.method !== "POST" || !url.pathname.endsWith("/api-key/create")) {
2026-05-28 10:18:28 +02:00
return null;
2026-06-27 17:08:38 +02:00
}
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user) {
return null;
}
// @ts-ignore
if (session.user.banned || (session.user.role as string) === "pending") {
return NextResponse.json(
{ error: "Account not eligible to create API keys" },
{ status: 403 },
);
}
return null;
2026-05-28 10:18:28 +02:00
}
export async function GET(req: NextRequest) {
2026-06-27 17:08:38 +02:00
return authHandler.GET(req);
2026-05-28 10:18:28 +02:00
}
export async function POST(req: NextRequest) {
2026-06-27 17:08:38 +02:00
const guard = await blockApiKeyCreateForRestrictedUsers(req);
if (guard) return guard;
return authHandler.POST(req);
2026-05-28 10:18:28 +02:00
}