@@ -3,6 +3,7 @@ import * as schema from '@codebuff/common/db/schema'
33import { validateAgents } from '@codebuff/common/templates/agent-validation'
44import { DynamicAgentTemplateSchema } from '@codebuff/common/types/dynamic-agent-template'
55import {
6+ checkAuthToken ,
67 determineNextVersion ,
78 stringifyVersion ,
89 versionExists ,
@@ -12,29 +13,22 @@ import { NextResponse } from 'next/server'
1213import { getServerSession } from 'next-auth'
1314import { z } from 'zod'
1415
16+ import { logger } from '@/util/logger'
17+
1518import { authOptions } from '../../auth/[...nextauth]/auth-options'
1619
1720import type { Version } from '@codebuff/internal'
1821import type { NextRequest } from 'next/server'
1922
20- import { logger } from '@/util/logger'
21-
2223// Schema for publishing an agent
2324const publishAgentRequestSchema = z . object ( {
2425 data : DynamicAgentTemplateSchema ,
2526 publisherId : z . string ( ) . optional ( ) ,
27+ authToken : z . string ( ) ,
2628} )
2729
2830export async function POST ( request : NextRequest ) {
2931 try {
30- // Check authentication
31- const session = await getServerSession ( authOptions )
32- if ( ! session ?. user ?. id ) {
33- return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
34- }
35-
36- const userId = session . user . id
37-
3832 // Parse request body
3933 const body = await request . json ( )
4034 const parseResult = publishAgentRequestSchema . safeParse ( body )
@@ -54,9 +48,26 @@ export async function POST(request: NextRequest) {
5448 )
5549 }
5650
57- const { data, publisherId } = parseResult . data
51+ const { data, publisherId, authToken } = parseResult . data
5852 const agentId = data . id
5953
54+ // Try cookie-based auth first, then fall back to authToken validation using proper function
55+ let userId : string | undefined
56+ const session = await getServerSession ( authOptions )
57+
58+ if ( session ?. user ?. id ) {
59+ userId = session . user . id
60+ } else if ( authToken ) {
61+ const authResult = await checkAuthToken ( { authToken } )
62+ if ( authResult . success && authResult . user ) {
63+ userId = authResult . user . id
64+ }
65+ }
66+
67+ if ( ! userId ) {
68+ return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
69+ }
70+
6071 const validationResult = validateAgents ( {
6172 [ agentId ] : data ,
6273 } )
0 commit comments