fix some old auth

This commit is contained in:
songtianlun 2025-08-30 13:14:19 +08:00
parent 8b9ff9c376
commit f8ce3ee759
4 changed files with 42 additions and 42 deletions

View File

@ -1,5 +1,6 @@
import { NextResponse } from 'next/server' 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) { export async function POST(request: Request) {
try { try {
@ -20,14 +21,11 @@ export async function POST(request: Request) {
) )
} }
const supabase = await createServerSupabaseClient() const session = await auth.api.getSession({ headers: await headers() })
if (!session?.user) {
// 获取当前用户
const { data: { user }, error: authError } = await supabase.auth.getUser()
if (authError || !user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
} }
const user = session.user
// 统一重定向到 Stripe 支付 // 统一重定向到 Stripe 支付
return NextResponse.json( return NextResponse.json(

View File

@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from "next/server"; 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 { prisma } from "@/lib/prisma";
import { getPromptContent, calculateCost } from "@/lib/simulator-utils"; import { getPromptContent, calculateCost } from "@/lib/simulator-utils";
import { consumeCreditForSimulation, getUserBalance } from "@/lib/services/credit"; import { consumeCreditForSimulation, getUserBalance } from "@/lib/services/credit";
@ -12,12 +13,11 @@ export async function POST(
) { ) {
const { id } = await params; const { id } = await params;
try { try {
const supabase = await createServerSupabaseClient(); const session = await auth.api.getSession({ headers: await headers() });
const { data: { user }, error: authError } = await supabase.auth.getUser(); if (!session?.user) {
if (authError || !user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
} }
const user = session.user;
const run = await prisma.simulatorRun.findFirst({ const run = await prisma.simulatorRun.findFirst({
where: { where: {

View File

@ -1,15 +1,15 @@
import { NextResponse } from "next/server"; 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"; import { prisma } from "@/lib/prisma";
export async function GET() { export async function GET() {
try { try {
const supabase = await createServerSupabaseClient(); const session = await auth.api.getSession({ headers: await headers() });
const { data: { user }, error: authError } = await supabase.auth.getUser(); if (!session?.user) {
if (authError || !user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
} }
const user = session.user;
// 获取所有激活的模型(不再与套餐绑定) // 获取所有激活的模型(不再与套餐绑定)
const allModels = await prisma.model.findMany({ const allModels = await prisma.model.findMany({

View File

@ -1,6 +1,8 @@
import { NextResponse } from 'next/server' 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 { createCustomerPortalSession } from '@/lib/stripe'
import { prisma } from '@/lib/prisma'
export async function POST() { export async function POST() {
try { try {
@ -16,41 +18,41 @@ export async function POST() {
) )
} }
const supabase = await createServerSupabaseClient() const session = await auth.api.getSession({ headers: await headers() })
if (!session?.user) {
// 获取当前用户 console.log('❌ Auth error: No session')
const { data: { user }, error: authError } = await supabase.auth.getUser()
if (authError || !user) {
console.log('❌ Auth error:', authError)
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
} }
const user = session.user
console.log('👤 User ID:', user.id) console.log('👤 User ID:', user.id)
// 从订阅记录中获取Stripe客户ID // 从订阅记录中获取Stripe客户ID
const { data: subscriptionData, error: subscriptionError } = await supabase const subscriptionData = await prisma.subscription.findFirst({
.from('subscriptions') where: {
.select('stripeCustomerId') userId: user.id,
.eq('userId', user.id) isActive: true
.eq('isActive', true) },
.order('createdAt', { ascending: false }) orderBy: {
.limit(1) createdAt: 'desc'
.single() },
select: {
stripeCustomerId: true
}
})
if (subscriptionError || !subscriptionData?.stripeCustomerId) { if (!subscriptionData?.stripeCustomerId) {
console.log('⚠️ Subscription error or no customer ID:', subscriptionError, subscriptionData) console.log('⚠️ No customer ID in subscription:', subscriptionData)
// 如果没有活跃订阅,尝试从用户表获取 // 如果没有活跃订阅,尝试从用户表获取
const { data: userData, error: userError } = await supabase const userData = await prisma.user.findUnique({
.from('users') where: { id: user.id },
.select('stripeCustomerId') select: { stripeCustomerId: true }
.eq('id', user.id) })
.single()
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') console.log('❌ No Stripe customer ID found in either table')
return NextResponse.json( return NextResponse.json(
{ error: 'No Stripe customer found. Please create a subscription first.' }, { error: 'No Stripe customer found. Please create a subscription first.' },