diff --git a/app/(auth)/auth.config.ts b/app/(auth)/auth.config.ts index b8bc9e1f17..b03fad0431 100644 --- a/app/(auth)/auth.config.ts +++ b/app/(auth)/auth.config.ts @@ -1,6 +1,7 @@ import type { NextAuthConfig } from "next-auth"; export const authConfig = { + basePath: "/api/auth", pages: { signIn: "/login", newUser: "/", diff --git a/app/(chat)/api/chat/route.ts b/app/(chat)/api/chat/route.ts index 3a0c7b55eb..1f0b5b212c 100644 --- a/app/(chat)/api/chat/route.ts +++ b/app/(chat)/api/chat/route.ts @@ -1,4 +1,3 @@ -import { checkBotId } from "botid/server"; import { geolocation, ipAddress } from "@vercel/functions"; import { convertToModelMessages, @@ -8,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"; @@ -64,9 +64,12 @@ export async function POST(request: Request) { const { id, message, messages, selectedChatModel, selectedVisibilityType } = requestBody; - const [botResult, session] = await Promise.all([checkBotId(), auth()]); + const [botResult, session] = await Promise.all([ + checkBotId().catch(() => null), + auth(), + ]); - if (botResult.isBot) { + if (botResult?.isBot) { return new ChatbotError("unauthorized:chat").toResponse(); } diff --git a/instrumentation-client.ts b/instrumentation-client.ts index d3ed55a747..2f899da383 100644 --- a/instrumentation-client.ts +++ b/instrumentation-client.ts @@ -3,7 +3,7 @@ import { initBotId } from "botid/client/core"; initBotId({ protect: [ { - path: "/api/chat", + path: `${process.env.NEXT_PUBLIC_BASE_PATH ?? ""}/api/chat`, method: "POST", }, ], diff --git a/lib/ratelimit.ts b/lib/ratelimit.ts index cc75ab781d..fa0cc471bb 100644 --- a/lib/ratelimit.ts +++ b/lib/ratelimit.ts @@ -11,7 +11,7 @@ let client: ReturnType | null = null; function getClient() { if (!client && process.env.REDIS_URL) { client = createClient({ url: process.env.REDIS_URL }); - client.on("error", () => {}); + client.on("error", () => undefined); client.connect().catch(() => { client = null; }); @@ -20,10 +20,14 @@ function getClient() { } export async function checkIpRateLimit(ip: string | undefined) { - if (!isProductionEnvironment || !ip) return; + if (!isProductionEnvironment || !ip) { + return; + } const redis = getClient(); - if (!redis?.isReady) return; + if (!redis?.isReady) { + return; + } try { const key = `ip-rate-limit:${ip}`; @@ -37,6 +41,8 @@ export async function checkIpRateLimit(ip: string | undefined) { throw new ChatbotError("rate_limit:chat"); } } catch (error) { - if (error instanceof ChatbotError) throw error; + if (error instanceof ChatbotError) { + throw error; + } } } diff --git a/next.config.ts b/next.config.ts index 41196c8728..2a42780de1 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,5 +1,5 @@ -import type { NextConfig } from "next"; import { withBotId } from "botid/next/config"; +import type { NextConfig } from "next"; const basePath = "/demo"; @@ -8,6 +8,7 @@ const nextConfig: NextConfig = { assetPrefix: "/demo-assets", env: { NEXT_PUBLIC_BASE_PATH: basePath, + NEXTAUTH_URL: `http://localhost${basePath}/api/auth`, }, cacheComponents: true, images: { diff --git a/vercel.json b/vercel.json index ced26d3924..62abe29168 100644 --- a/vercel.json +++ b/vercel.json @@ -1,5 +1,15 @@ { "framework": "nextjs", + "rewrites": [ + { + "source": "/149e9513-01fa-4fb0-aad4-566afd725d1b/2d206a39-8ed7-437e-a3be-862e0f06eea3/a-4-a/c.js", + "destination": "https://api.vercel.com/bot-protection/v1/challenge" + }, + { + "source": "/149e9513-01fa-4fb0-aad4-566afd725d1b/2d206a39-8ed7-437e-a3be-862e0f06eea3/:path*", + "destination": "https://api.vercel.com/bot-protection/v1/proxy/:path*" + } + ], "redirects": [ { "source": "/",