cg / hackerproducts
63 files · 5,740 lines · 159.1 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.
Quick jump: page.tsxpage.tsxglobals.cssschema.sqlpackage.json
root / src / app / api / products / route.ts
src/app/api/products/route.ts · 95 lines · 3.0 KB← back to folder
| 1 | import crypto from "node:crypto"; |
| 2 | import { NextResponse } from "next/server"; |
| 3 | import { query } from "@/db"; |
| 4 | import { getCurrentUser } from "@/lib/auth"; |
| 5 | import { |
| 6 | getProductSubmissionCooldown, |
| 7 | normalizeWebsiteForDedup, |
| 8 | PRODUCT_COOLDOWN_DAYS, |
| 9 | } from "@/lib/antispam"; |
| 10 | import { generateUniqueSlug } from "@/lib/products"; |
| 11 | import { validateProductInput } from "@/lib/validation"; |
| 12 | |
| 13 | export async function POST(request: Request) { |
| 14 | const user = await getCurrentUser(); |
| 15 | if (!user) { |
| 16 | return NextResponse.json({ error: "Sign in required." }, { status: 401 }); |
| 17 | } |
| 18 | |
| 19 | const body = (await request.json().catch(() => null)) as { |
| 20 | name?: string; |
| 21 | website_url?: string; |
| 22 | description?: string; |
| 23 | readme_md?: string; |
| 24 | icon_url?: string | null; |
| 25 | screenshot_url?: string | null; |
| 26 | company_url?: string; // honeypot |
| 27 | } | null; |
| 28 | |
| 29 | if (!body) { |
| 30 | return NextResponse.json({ error: "Invalid request body." }, { status: 400 }); |
| 31 | } |
| 32 | |
| 33 | // Honeypot check |
| 34 | if (body.company_url && body.company_url.trim().length > 0) { |
| 35 | return NextResponse.json({ error: "Submission rejected." }, { status: 400 }); |
| 36 | } |
| 37 | |
| 38 | const validated = validateProductInput(body); |
| 39 | if (!validated.ok) { |
| 40 | return NextResponse.json({ error: validated.error }, { status: 400 }); |
| 41 | } |
| 42 | |
| 43 | // Anti-spam: 1 product per user every 7 days + duplicate URL check (admins exempt) |
| 44 | if (!user.is_admin) { |
| 45 | const cooldown = await getProductSubmissionCooldown(user.id); |
| 46 | if (!cooldown.allowed) { |
| 47 | const daysText = |
| 48 | cooldown.daysRemaining === 1 ? "1 day" : `${cooldown.daysRemaining} days`; |
| 49 | return NextResponse.json( |
| 50 | { |
| 51 | error: `To prevent spam, each maker can submit one product every ${PRODUCT_COOLDOWN_DAYS} days. You can publish your next product in ${daysText}.`, |
| 52 | }, |
| 53 | { status: 429 } |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | const incomingKey = normalizeWebsiteForDedup(validated.value.website_url); |
| 58 | const activeUrls = await query<{ website_url: string }>( |
| 59 | `SELECT website_url FROM products WHERE status IN ('published', 'pending')` |
| 60 | ); |
| 61 | const duplicate = activeUrls.rows.some( |
| 62 | (r) => normalizeWebsiteForDedup(r.website_url) === incomingKey |
| 63 | ); |
| 64 | if (duplicate) { |
| 65 | return NextResponse.json( |
| 66 | { error: "A product with this website URL has already been submitted." }, |
| 67 | { status: 409 } |
| 68 | ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | const slug = await generateUniqueSlug(validated.value.name); |
| 73 | const id = crypto.randomUUID(); |
| 74 | const initialStatus = process.env.REQUIRE_MODERATION === "true" ? "pending" : "published"; |
| 75 | |
| 76 | await query( |
| 77 | `INSERT INTO products (id, owner_id, name, slug, website_url, description, readme_md, icon_url, screenshot_url, status) |
| 78 | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`, |
| 79 | [ |
| 80 | id, |
| 81 | user.id, |
| 82 | validated.value.name, |
| 83 | slug, |
| 84 | validated.value.website_url, |
| 85 | validated.value.description, |
| 86 | validated.value.readme_md, |
| 87 | validated.value.icon_url, |
| 88 | validated.value.screenshot_url, |
| 89 | initialStatus, |
| 90 | ] |
| 91 | ); |
| 92 | |
| 93 | return NextResponse.json({ id, slug, status: initialStatus }, { status: 201 }); |
| 94 | } |
| 95 |