3 comics per week
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@clerk/nextjs/server";
|
||||
import { freeTierRateLimit } from "@/lib/rate-limit";
|
||||
import { db } from "@/lib/db";
|
||||
import { stories } from "@/lib/schema";
|
||||
import { gte } from "drizzle-orm";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
@@ -27,36 +24,14 @@ export async function POST(request: NextRequest) {
|
||||
});
|
||||
}
|
||||
|
||||
// For free tier, check rate limit status
|
||||
try {
|
||||
// Try to check remaining without consuming
|
||||
const limitResult = await freeTierRateLimit.getRemaining(userId);
|
||||
// For free tier, check rate limit status via Redis
|
||||
const limitResult = await freeTierRateLimit.getRemaining(userId);
|
||||
|
||||
return NextResponse.json({
|
||||
hasApiKey: false,
|
||||
creditsRemaining: limitResult.remaining,
|
||||
resetTime: limitResult.reset,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Rate limit check error:", error);
|
||||
// Fallback to database check
|
||||
const sevenDaysAgo = new Date();
|
||||
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
|
||||
|
||||
const recentStories = await db
|
||||
.select()
|
||||
.from(stories)
|
||||
.where(gte(stories.createdAt, sevenDaysAgo))
|
||||
.limit(1);
|
||||
|
||||
const hasUsedCredit = recentStories.length > 0;
|
||||
|
||||
return NextResponse.json({
|
||||
hasApiKey: false,
|
||||
creditsRemaining: hasUsedCredit ? 0 : 1,
|
||||
resetTime: hasUsedCredit ? sevenDaysAgo.getTime() + 7 * 24 * 60 * 60 * 1000 : null,
|
||||
});
|
||||
}
|
||||
return NextResponse.json({
|
||||
hasApiKey: false,
|
||||
creditsRemaining: limitResult.remaining,
|
||||
resetTime: limitResult.reset,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error in check-credits API:", error);
|
||||
return NextResponse.json(
|
||||
@@ -68,4 +43,4 @@ export async function POST(request: NextRequest) {
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,7 +204,7 @@ export function StoryEditorClient() {
|
||||
setShowApiModal(true);
|
||||
toast({
|
||||
title: "No credits remaining",
|
||||
description: "You get 1 credit weekly. Add an API key for unlimited generation.",
|
||||
description: "You get 3 credits weekly. Add an API key for unlimited generation.",
|
||||
variant: "destructive",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ export function ApiKeyModal({ isOpen, onClose, onSubmit }: ApiKeyModalProps) {
|
||||
<DialogDescription className="text-center text-muted-foreground">
|
||||
{existingKey
|
||||
? "Update your Together API key or add a new one. You can also delete your existing key."
|
||||
: "You've used your weekly credit! Add your Together API key for unlimited generation."}
|
||||
: "You've used all your weekly credits! Add your Together API key for unlimited generation."}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
|
||||
+2
-2
@@ -6,10 +6,10 @@ const redis = new Redis({
|
||||
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
|
||||
})
|
||||
|
||||
// Free tier: 1 comic per week (fixed window)
|
||||
// Free tier: 3 comics per week (fixed window)
|
||||
export const freeTierRateLimit = new Ratelimit({
|
||||
redis,
|
||||
limiter: Ratelimit.fixedWindow(1, "7 d"),
|
||||
limiter: Ratelimit.fixedWindow(3, "7 d"),
|
||||
analytics: true,
|
||||
prefix: "ratelimit:free-comics",
|
||||
})
|
||||
Reference in New Issue
Block a user