Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
stepCountIs,
streamText,
} from "ai";
import { checkBotId } from "botid/server";
import { after } from "next/server";
import { createResumableStreamContext } from "resumable-stream";
import { auth, type UserType } from "@/app/(auth)/auth";
Expand Down Expand Up @@ -63,7 +64,14 @@ export async function POST(request: Request) {
const { id, message, messages, selectedChatModel, selectedVisibilityType } =
requestBody;

const session = await auth();
const [botResult, session] = await Promise.all([
checkBotId().catch(() => null),
auth(),
]);

if (botResult?.isBot) {
return new ChatbotError("unauthorized:chat").toResponse();
}

if (!session?.user) {
return new ChatbotError("unauthorized:chat").toResponse();
Expand Down
11 changes: 10 additions & 1 deletion instrumentation-client.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export {};
import { initBotId } from "botid/client/core";

initBotId({
protect: [
{
path: `${process.env.NEXT_PUBLIC_BASE_PATH ?? ""}/api/chat`,
method: "POST",
},
],
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@xyflow/react": "^12.10.0",
"ai": "6.0.37",
"bcrypt-ts": "^5.0.2",
"botid": "^1.5.10",
"class-variance-authority": "^0.7.1",
"classnames": "^2.5.1",
"clsx": "^2.1.1",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@ import { type NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
import { guestRegex, isDevelopmentEnvironment } from "./lib/constants";

const BOTID_PREFIX =
"/149e9513-01fa-4fb0-aad4-566afd725d1b/2d206a39-8ed7-437e-a3be-862e0f06eea3";

export async function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;

/*
* Playwright starts the dev server and requires a 200 status to
* begin the tests, so this ensures that the tests can start
*/
if (pathname.startsWith(BOTID_PREFIX)) {
const suffix = pathname.slice(BOTID_PREFIX.length);
if (suffix === "/a-4-a/c.js") {
return NextResponse.rewrite(
new URL(
`https://api.vercel.com/bot-protection/v1/challenge${request.nextUrl.search}`,
),
);
}
return NextResponse.rewrite(
new URL(
`https://api.vercel.com/bot-protection/v1/proxy${suffix}${request.nextUrl.search}`,
),
);
}

if (pathname.startsWith("/ping")) {
return new Response("pong", { status: 200 });
}
Expand Down
Loading