fix: language default, google OAuth cookie issue, add admin tier/usage controls

- ai-generate-dialog: useLocale() returns {locale,setLocale} object, not
  string — was stringifying whole object as default language value
- auth/server: add trustedOrigins so session cookie survives reverse-proxy
  deployments where BETTER_AUTH_URL differs from container's own view
- admin: add tier limit editor (/admin/tiers) and per-user usage reset
  button, both audit-logged

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Arnaud
2026-07-03 19:51:06 +02:00
parent cbba1ec2ff
commit ac9f5c87e9
9 changed files with 244 additions and 2 deletions
@@ -0,0 +1,55 @@
import { NextRequest, NextResponse } from "next/server";
import { requireAdmin } from "@/lib/api-auth";
import { db, tierDefinitions, auditLogs, eq } from "@epicure/db";
import { randomUUID } from "crypto";
interface RouteContext {
params: Promise<{ tier: string }>;
}
const NUMERIC_FIELDS = ["maxRecipes", "aiCallsPerMonth", "storageMb", "maxPublicRecipes"] as const;
type NumericField = (typeof NUMERIC_FIELDS)[number];
export async function PATCH(req: NextRequest, { params }: RouteContext) {
const { session, response } = await requireAdmin();
if (response) return response;
const { tier } = await params;
if (tier !== "free" && tier !== "pro") {
return NextResponse.json({ error: "Invalid tier" }, { status: 400 });
}
const body = (await req.json()) as Partial<Record<NumericField, number>>;
const updateData: Partial<Record<NumericField, number>> = {};
for (const field of NUMERIC_FIELDS) {
const value = body[field];
if (value === undefined) continue;
if (!Number.isInteger(value) || value < 0) {
return NextResponse.json({ error: `Invalid value for ${field}` }, { status: 400 });
}
updateData[field] = value;
}
const [updated] = await db
.update(tierDefinitions)
.set(updateData)
.where(eq(tierDefinitions.tier, tier))
.returning();
if (!updated) {
return NextResponse.json({ error: "Tier not found" }, { status: 404 });
}
await db.insert(auditLogs).values({
id: randomUUID(),
userId: session!.user.id,
action: "admin.tier.update",
targetType: "tier_definition",
targetId: tier,
metadata: JSON.stringify(updateData),
createdAt: new Date(),
});
return NextResponse.json({ tierDefinition: updated });
}
@@ -0,0 +1,39 @@
import { NextRequest, NextResponse } from "next/server";
import { requireAdmin } from "@/lib/api-auth";
import { db, userUsage, auditLogs, eq, and } from "@epicure/db";
import { randomUUID } from "crypto";
interface RouteContext {
params: Promise<{ id: string }>;
}
function currentMonth() {
const now = new Date();
return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}`;
}
export async function PATCH(req: NextRequest, { params }: RouteContext) {
const { session, response } = await requireAdmin();
if (response) return response;
const { id } = await params;
const month = currentMonth();
const [updated] = await db
.update(userUsage)
.set({ aiCallsUsed: 0, recipeCount: 0, storageUsedMb: 0 })
.where(and(eq(userUsage.userId, id), eq(userUsage.month, month)))
.returning();
await db.insert(auditLogs).values({
id: randomUUID(),
userId: session!.user.id,
action: "admin.user.reset_usage",
targetType: "user",
targetId: id,
metadata: JSON.stringify({ month }),
createdAt: new Date(),
});
return NextResponse.json({ usage: updated ?? { userId: id, month, aiCallsUsed: 0, recipeCount: 0, storageUsedMb: 0 } });
}