|
1 | | -import { type NextRequest, NextResponse } from 'next/server' |
2 | | -import { getSession } from '@/lib/auth' |
| 1 | +import { NextResponse } from 'next/server' |
| 2 | +import { authorizeCredentialUse } from '@/lib/auth/credential-access' |
| 3 | +import { generateRequestId } from '@/lib/core/utils/request' |
3 | 4 | import { createLogger } from '@/lib/logs/console/logger' |
4 | | -import { getOAuthToken } from '@/app/api/auth/oauth/utils' |
| 5 | +import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils' |
5 | 6 |
|
6 | 7 | const logger = createLogger('WebflowCollectionsAPI') |
7 | 8 |
|
8 | 9 | export const dynamic = 'force-dynamic' |
9 | 10 |
|
10 | | -export async function GET(request: NextRequest) { |
| 11 | +export async function POST(request: Request) { |
11 | 12 | try { |
12 | | - const session = await getSession() |
13 | | - if (!session?.user?.id) { |
14 | | - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
15 | | - } |
| 13 | + const requestId = generateRequestId() |
| 14 | + const body = await request.json() |
| 15 | + const { credential, workflowId, siteId } = body |
16 | 16 |
|
17 | | - const { searchParams } = new URL(request.url) |
18 | | - const siteId = searchParams.get('siteId') |
| 17 | + if (!credential) { |
| 18 | + logger.error('Missing credential in request') |
| 19 | + return NextResponse.json({ error: 'Credential is required' }, { status: 400 }) |
| 20 | + } |
19 | 21 |
|
20 | 22 | if (!siteId) { |
21 | | - return NextResponse.json({ error: 'Missing siteId parameter' }, { status: 400 }) |
| 23 | + logger.error('Missing siteId in request') |
| 24 | + return NextResponse.json({ error: 'Site ID is required' }, { status: 400 }) |
22 | 25 | } |
23 | 26 |
|
24 | | - const accessToken = await getOAuthToken(session.user.id, 'webflow') |
| 27 | + const authz = await authorizeCredentialUse(request as any, { |
| 28 | + credentialId: credential, |
| 29 | + workflowId, |
| 30 | + }) |
| 31 | + if (!authz.ok || !authz.credentialOwnerUserId) { |
| 32 | + return NextResponse.json({ error: authz.error || 'Unauthorized' }, { status: 403 }) |
| 33 | + } |
25 | 34 |
|
| 35 | + const accessToken = await refreshAccessTokenIfNeeded( |
| 36 | + credential, |
| 37 | + authz.credentialOwnerUserId, |
| 38 | + requestId |
| 39 | + ) |
26 | 40 | if (!accessToken) { |
| 41 | + logger.error('Failed to get access token', { |
| 42 | + credentialId: credential, |
| 43 | + userId: authz.credentialOwnerUserId, |
| 44 | + }) |
27 | 45 | return NextResponse.json( |
28 | | - { error: 'No Webflow access token found. Please connect your Webflow account.' }, |
29 | | - { status: 404 } |
| 46 | + { |
| 47 | + error: 'Could not retrieve access token', |
| 48 | + authRequired: true, |
| 49 | + }, |
| 50 | + { status: 401 } |
30 | 51 | ) |
31 | 52 | } |
32 | 53 |
|
@@ -58,11 +79,11 @@ export async function GET(request: NextRequest) { |
58 | 79 | name: collection.displayName || collection.slug || collection.id, |
59 | 80 | })) |
60 | 81 |
|
61 | | - return NextResponse.json({ collections: formattedCollections }, { status: 200 }) |
62 | | - } catch (error: any) { |
63 | | - logger.error('Error fetching Webflow collections', error) |
| 82 | + return NextResponse.json({ collections: formattedCollections }) |
| 83 | + } catch (error) { |
| 84 | + logger.error('Error processing Webflow collections request:', error) |
64 | 85 | return NextResponse.json( |
65 | | - { error: 'Internal server error', details: error.message }, |
| 86 | + { error: 'Failed to retrieve Webflow collections', details: (error as Error).message }, |
66 | 87 | { status: 500 } |
67 | 88 | ) |
68 | 89 | } |
|
0 commit comments