diff --git a/src/app/api/credits/topup/route.ts b/src/app/api/credits/topup/route.ts index c72305e..a515dfb 100644 --- a/src/app/api/credits/topup/route.ts +++ b/src/app/api/credits/topup/route.ts @@ -1,5 +1,6 @@ import { NextResponse } from 'next/server' -import { createServerSupabaseClient } from '@/lib/supabase-server' +import { auth } from '@/lib/auth' +import { headers } from 'next/headers' export async function POST(request: Request) { try { @@ -20,14 +21,11 @@ export async function POST(request: Request) { ) } - const supabase = await createServerSupabaseClient() - - // 获取当前用户 - const { data: { user }, error: authError } = await supabase.auth.getUser() - - if (authError || !user) { + const session = await auth.api.getSession({ headers: await headers() }) + if (!session?.user) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } + const user = session.user // 统一重定向到 Stripe 支付 return NextResponse.json( diff --git a/src/app/api/simulator/[id]/execute/route.ts b/src/app/api/simulator/[id]/execute/route.ts index e6911f3..31f307e 100644 --- a/src/app/api/simulator/[id]/execute/route.ts +++ b/src/app/api/simulator/[id]/execute/route.ts @@ -1,5 +1,6 @@ import { NextRequest, NextResponse } from "next/server"; -import { createServerSupabaseClient } from "@/lib/supabase-server"; +import { auth } from "@/lib/auth"; +import { headers } from "next/headers"; import { prisma } from "@/lib/prisma"; import { getPromptContent, calculateCost } from "@/lib/simulator-utils"; import { consumeCreditForSimulation, getUserBalance } from "@/lib/services/credit"; @@ -12,12 +13,11 @@ export async function POST( ) { const { id } = await params; try { - const supabase = await createServerSupabaseClient(); - const { data: { user }, error: authError } = await supabase.auth.getUser(); - - if (authError || !user) { + const session = await auth.api.getSession({ headers: await headers() }); + if (!session?.user) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } + const user = session.user; const run = await prisma.simulatorRun.findFirst({ where: { diff --git a/src/app/api/simulator/models/route.ts b/src/app/api/simulator/models/route.ts index 7daf4c7..1de8d4d 100644 --- a/src/app/api/simulator/models/route.ts +++ b/src/app/api/simulator/models/route.ts @@ -1,15 +1,15 @@ import { NextResponse } from "next/server"; -import { createServerSupabaseClient } from "@/lib/supabase-server"; +import { auth } from "@/lib/auth"; +import { headers } from "next/headers"; import { prisma } from "@/lib/prisma"; export async function GET() { try { - const supabase = await createServerSupabaseClient(); - const { data: { user }, error: authError } = await supabase.auth.getUser(); - - if (authError || !user) { + const session = await auth.api.getSession({ headers: await headers() }); + if (!session?.user) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } + const user = session.user; // 获取所有激活的模型(不再与套餐绑定) const allModels = await prisma.model.findMany({ diff --git a/src/app/api/subscription/portal/route.ts b/src/app/api/subscription/portal/route.ts index 7f4c110..1c3db34 100644 --- a/src/app/api/subscription/portal/route.ts +++ b/src/app/api/subscription/portal/route.ts @@ -1,6 +1,8 @@ import { NextResponse } from 'next/server' -import { createServerSupabaseClient } from '@/lib/supabase-server' +import { auth } from '@/lib/auth' +import { headers } from 'next/headers' import { createCustomerPortalSession } from '@/lib/stripe' +import { prisma } from '@/lib/prisma' export async function POST() { try { @@ -16,41 +18,41 @@ export async function POST() { ) } - const supabase = await createServerSupabaseClient() - - // 获取当前用户 - const { data: { user }, error: authError } = await supabase.auth.getUser() - - if (authError || !user) { - console.log('❌ Auth error:', authError) + const session = await auth.api.getSession({ headers: await headers() }) + if (!session?.user) { + console.log('❌ Auth error: No session') return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } + const user = session.user console.log('👤 User ID:', user.id) // 从订阅记录中获取Stripe客户ID - const { data: subscriptionData, error: subscriptionError } = await supabase - .from('subscriptions') - .select('stripeCustomerId') - .eq('userId', user.id) - .eq('isActive', true) - .order('createdAt', { ascending: false }) - .limit(1) - .single() + const subscriptionData = await prisma.subscription.findFirst({ + where: { + userId: user.id, + isActive: true + }, + orderBy: { + createdAt: 'desc' + }, + select: { + stripeCustomerId: true + } + }) - if (subscriptionError || !subscriptionData?.stripeCustomerId) { - console.log('⚠️ Subscription error or no customer ID:', subscriptionError, subscriptionData) + if (!subscriptionData?.stripeCustomerId) { + console.log('⚠️ No customer ID in subscription:', subscriptionData) // 如果没有活跃订阅,尝试从用户表获取 - const { data: userData, error: userError } = await supabase - .from('users') - .select('stripeCustomerId') - .eq('id', user.id) - .single() + const userData = await prisma.user.findUnique({ + where: { id: user.id }, + select: { stripeCustomerId: true } + }) - console.log('📋 User data from users table:', userData, userError) + console.log('📋 User data from users table:', userData) - if (userError || !userData?.stripeCustomerId) { + if (!userData?.stripeCustomerId) { console.log('❌ No Stripe customer ID found in either table') return NextResponse.json( { error: 'No Stripe customer found. Please create a subscription first.' },