Skip to content
Merged
1 change: 1 addition & 0 deletions app/(auth)/auth.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NextAuthConfig } from "next-auth";

export const authConfig = {
basePath: "/api/auth",
pages: {
signIn: "/login",
newUser: "/",
Expand Down
9 changes: 6 additions & 3 deletions app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { checkBotId } from "botid/server";
import { geolocation, ipAddress } from "@vercel/functions";
import {
convertToModelMessages,
Expand All @@ -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";
Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion instrumentation-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
],
Expand Down
14 changes: 10 additions & 4 deletions lib/ratelimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let client: ReturnType<typeof createClient> | 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;
});
Expand All @@ -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}`;
Expand All @@ -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;
}
}
}
3 changes: 2 additions & 1 deletion next.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextConfig } from "next";
import { withBotId } from "botid/next/config";
import type { NextConfig } from "next";

const basePath = "/demo";

Expand All @@ -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: {
Expand Down
10 changes: 10 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -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": "/",
Expand Down
Loading