Skip to content

Commit 6ebf565

Browse files
committed
fix: make agents/publish api check cookies and then user
1 parent 132b473 commit 6ebf565

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

web/src/app/api/agents/publish/route.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as schema from '@codebuff/common/db/schema'
33
import { validateAgents } from '@codebuff/common/templates/agent-validation'
44
import { DynamicAgentTemplateSchema } from '@codebuff/common/types/dynamic-agent-template'
55
import {
6+
checkAuthToken,
67
determineNextVersion,
78
stringifyVersion,
89
versionExists,
@@ -12,29 +13,22 @@ import { NextResponse } from 'next/server'
1213
import { getServerSession } from 'next-auth'
1314
import { z } from 'zod'
1415

16+
import { logger } from '@/util/logger'
17+
1518
import { authOptions } from '../../auth/[...nextauth]/auth-options'
1619

1720
import type { Version } from '@codebuff/internal'
1821
import type { NextRequest } from 'next/server'
1922

20-
import { logger } from '@/util/logger'
21-
2223
// Schema for publishing an agent
2324
const publishAgentRequestSchema = z.object({
2425
data: DynamicAgentTemplateSchema,
2526
publisherId: z.string().optional(),
27+
authToken: z.string(),
2628
})
2729

2830
export 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

Comments
 (0)