Initial import: property management SaaS + security hardening + admin dashboard
Property Management Network — Next.js 16 (App Router), Better Auth, Drizzle ORM over PostgreSQL, Stripe, OpenAI, Resend. Includes: - Security hardening: access-control/IDOR fixes, TLS-by-default DB layer, constant-time cron auth, strict security headers, atomic AI quota gating, HTML/email output encoding, demo-backdoor disabled in production. - Superadmin dashboard at /admin (overview/MRR, server-paginated users with ban/impersonate/plan/delete, billing, platform activity + admin audit log, AI usage, system health) via the Better Auth admin plugin. - Seed/migration utility scripts under scripts/. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { eq } from "drizzle-orm"
|
||||
import { db } from "@/lib/db"
|
||||
import { profiles } from "@/lib/db/schema"
|
||||
import { getSessionUser } from "@/lib/session"
|
||||
import { stripe } from "@/lib/stripe/client"
|
||||
import { PLAN_PRICES } from "@/lib/stripe/plans"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const user = await getSessionUser()
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const { plan } = await request.json() as { plan: string }
|
||||
|
||||
const planConfig = PLAN_PRICES[plan]
|
||||
if (!planConfig) return NextResponse.json({ error: "Invalid plan" }, { status: 400 })
|
||||
|
||||
const profile = await db.query.profiles.findFirst({
|
||||
where: eq(profiles.id, user.id),
|
||||
columns: { stripe_customer_id: true, email: true, full_name: true },
|
||||
})
|
||||
|
||||
// Get or create Stripe customer
|
||||
let customerId = profile?.stripe_customer_id
|
||||
|
||||
if (!customerId) {
|
||||
const customer = await stripe.customers.create({
|
||||
email: profile?.email ?? user.email,
|
||||
name: profile?.full_name ?? undefined,
|
||||
metadata: { supabase_user_id: user.id },
|
||||
})
|
||||
customerId = customer.id
|
||||
|
||||
await db
|
||||
.update(profiles)
|
||||
.set({ stripe_customer_id: customerId })
|
||||
.where(eq(profiles.id, user.id))
|
||||
}
|
||||
|
||||
const isLifetime = planConfig.interval === "one_time"
|
||||
const appUrl = process.env.NEXT_PUBLIC_APP_URL!
|
||||
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
customer: customerId,
|
||||
mode: isLifetime ? "payment" : "subscription",
|
||||
line_items: [{ price: planConfig.priceId, quantity: 1 }],
|
||||
success_url: `${appUrl}/settings/billing?success=true`,
|
||||
cancel_url: `${appUrl}/settings/billing?canceled=true`,
|
||||
metadata: {
|
||||
supabase_user_id: user.id,
|
||||
plan: planConfig.plan,
|
||||
},
|
||||
...(isLifetime ? {} : {
|
||||
subscription_data: {
|
||||
metadata: { supabase_user_id: user.id, plan: planConfig.plan },
|
||||
},
|
||||
}),
|
||||
})
|
||||
|
||||
return NextResponse.json({ url: session.url })
|
||||
}
|
||||
Reference in New Issue
Block a user