chatbot-template/proxy.ts
dmitry.galkin 22a172e92d Add Umami analytics, subscription tiers, tool-call metering
- Analytics: <Analytics /> in root layout loads umami when NEXT_PUBLIC_UMAMI_* env are set
- Subscription: free/pro tiers in Postgres, daily quotas (10msg/5tool free, 1000/500 pro)
- Chat route: checkAndConsume(chat_message) gate before streamText, 429 when exhausted
- Tool metering: getWeather wrapped with withLimit decorator that deducts tool_call quota
- Pricing page at /pricing with upgrade button
- /api/billing/checkout posts to EGBE payment gateway, /api/billing/webhook verifies HMAC and upgrades user on checkout.session.completed
- UsageBadge in chat header polls /api/usage every 15s
- Dropped now-unused entitlements.ts
2026-05-25 17:27:08 +04:00

55 lines
1.3 KiB
TypeScript

import { type NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
import { guestRegex, isDevelopmentEnvironment } from "./lib/constants";
export async function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
if (pathname.startsWith("/ping")) {
return new Response("pong", { status: 200 });
}
if (pathname.startsWith("/api/auth")) {
return NextResponse.next();
}
if (pathname.startsWith("/api/billing/webhook")) {
return NextResponse.next();
}
const token = await getToken({
req: request,
secret: process.env.AUTH_SECRET,
secureCookie: !isDevelopmentEnvironment,
});
const base = process.env.NEXT_PUBLIC_BASE_PATH ?? "";
if (!token) {
const redirectUrl = encodeURIComponent(new URL(request.url).pathname);
return NextResponse.redirect(
new URL(`${base}/api/auth/guest?redirectUrl=${redirectUrl}`, request.url)
);
}
const isGuest = guestRegex.test(token?.email ?? "");
if (token && !isGuest && ["/login", "/register"].includes(pathname)) {
return NextResponse.redirect(new URL(`${base}/`, request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
"/",
"/chat/:id",
"/api/:path*",
"/login",
"/register",
"/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
],
};